开发者

How to focus on an alternate label using cocoa?

开发者 https://www.devze.com 2023-02-15 12:27 出处:网络
I\'m still a noob to iPhone development so sorry for the dumb question. I\'m creating an app that has a custom numeric keyboard开发者_StackOverflow中文版. I have an IBAction \'buttonDigitPressed\' tha

I'm still a noob to iPhone development so sorry for the dumb question. I'm creating an app that has a custom numeric keyboard开发者_StackOverflow中文版. I have an IBAction 'buttonDigitPressed' that when a digit is pressed it will simply add the digit to a UILabel. Now I have another label that I wish to do the same yet I'm not sure how to gain 'focus' of that particular label. I placed an invisible button over the second label so when pressed hopefully I can call an action to switch the keyboard from the first label to the second label. Yet I have no clue how to accomplish this. Any help would be greatly appreciated!


First of all, having to put an invisible button over the UILabel is skanky. If you want the user to be able to tap a number to mean "next time I hit a digit, append its value to this number", you'll probably be happier if you just use a UIButton right from the start. You can make a UIButton that looks pretty much like a UILabel (i.e. it has no border or background color). To set a UIButton's title in code, call setTitle:forState: with the UIControlStateNormal state. So now you've got tappable numbers, i.e. these UIButtons.

Okay, so now let's pretend you've got, say, three UIButtons. You need to use an instance variable here. So, each button will have an action - let's say doButton:(id)sender. So doButton will store the value of sender (the button that was just pressed) in the instance variable. Now your buttonDigitPressed action just looks in that instance variable to learn which button to append to. That's part of the power of instance variables - they give a method in a class a place to store a value where another method in the same class can get at it.

0

精彩评论

暂无评论...
验证码 换一张
取 消