Post not yet marked as solved
Hi,
I am trying to focus a textbox once a button is clicked, and I am not sure how to do this?
Present TextField
@State var isHidden: Bool = true
...
if !isHidden {
withAnimation{
TextField("Add Note...", text: $textValue)
.padding(.top, 10.0)
}
.transition(.move(edge: .trailing))
.textFieldStyle(RoundedBorderTextFieldStyle())
}
Button to show Text Field
if (buttonCount == 0){
isHidden = false
buttonCount += 1
}
At the moment everything works fine but when I click the button to show the textfield its not auto-selected by default.
MacOS Version: > 11.0
Post not yet marked as solved
I am trying to add a record which contains the order and text of it. I can add a new record within the same file using the function "addItem()", but when I try to call this same function from another file I get the error
Thread 1: EXCBADINSTRUCTION (code=EXCI386INVOP, subcode=0x0) on the line
newItem.order = (listItems.last?.order ?? 0) + 1.
var body: some View {
		NavigationView {
				VStack {
						List {
								ForEach(listItems, id: \.self) { item in
										NavigationLink(destination: ListItemView(listItem: item)) {
												Text("\(item.name) - \(item.order)")
										}
								}
								.onDelete(perform: deleteItem)
								.onMove(perform: moveItem)
						}
					 // Button(action: addItem) {
						//		Text("Add Item")
					 // }
						Button(action: {
								self.isShowingAlert.toggle()
						}) {
								Text("Add Item")
						}
				}
				.navigationBarItems(trailing: EditButton())
		}
		.addSujectPopUp(isShowing: $isShowingAlert, text: $alertInput, title: "Add Subject")
}
func moveItem(indexSet: IndexSet, destination: Int) {
		let source = indexSet.first!
		
		if source < destination {
				var startIndex = source + 1
				let endIndex = destination - 1
				var startOrder = listItems[source].order
				while startIndex <= endIndex {
						listItems[startIndex].order = startOrder
						startOrder = startOrder + 1
						startIndex = startIndex + 1
				}
				
				listItems[source].order = startOrder
				
		} else if destination < source {
				var startIndex = destination
				let endIndex = source - 1
				var startOrder = listItems[destination].order + 1
				let newOrder = listItems[destination].order
				while startIndex <= endIndex {
						listItems[startIndex].order = startOrder
						startOrder = startOrder + 1
						startIndex = startIndex + 1
				}
				listItems[source].order = newOrder
		}
		
		saveItems()
}
func deleteItem(indexSet: IndexSet) {
		let source = indexSet.first!
		let listItem = listItems[source]
		managedObjectContext.delete(listItem)
		saveItems()
}
func addItem() {
		let newItem = ListItem(context: managedObjectContext)
		newItem.name = alertInput
		newItem.order = (listItems.last?.order ?? 0) + 1
		saveItems()
}
func saveItems() {
		do {
				try managedObjectContext.save()
		} catch {
				print(error)
		}
}
Pop Up Code button:
let subject = SubjectController()
Button(action: self.subject.addItem) {
														Text("Add")
												}
I am currently making a game on Xcode 11, I am having a problem where in which my sprite will spawn in straight away at the start, even through their is a wait delay, but after the first spawn the sprites spawn in at their current time. How can I make it so the sprites wont spawn in untill 3 seconds after the scene has started. Any help would be greatly appreciated.The photo attached shows what happens at the start of the game.I have attached part of my code for one of the sprites as their setup is similar to each other.//Setup Bird
func setupBird() {
bird = SKSpriteNode(imageNamed: "bird-1")
bird.name = "Bird"
bird.zPosition = 20.0
bird.setScale(1.5)
let birdHeight = bird.frame.height
let random = CGFloat.random(min: -birdHeight, max: birdHeight*2.0)
bird.position = CGPoint(x: cameraRect.maxX + bird.frame.width, y: size.height/2.0 + birdHeight + random)
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.width/2.0)
bird.physicsBody!.affectedByGravity = false
bird.physicsBody!.isDynamic = false
bird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
bird.physicsBody!.contactTestBitMask = PhysicsCategory.Player
addChild(bird)
bird.run(.sequence([.wait(forDuration: 15, withRange: 5), .removeFromParent()]))
//Animation For Birds
var textures: [SKTexture] = []
for i in 1...3 {
textures.append(SKTexture(imageNamed: "bird-\(i)"))
}
bird.run(.repeatForever(.animate(with: textures, timePerFrame: 0.15)))
}
func spawnBird() {
let random = CGFloat.random(min: 15.0, max: 30.0)
run(.repeatForever(.sequence([
.wait(forDuration: TimeInterval(random)),
.run { [weak self] in
self?.setupBird()
}
])))
}