I am creating iphone application. How to trigger an event when button click?
in xxx.h
UIButton *b1, *b2, *b3;
in xxx.m
b1 = ---- similarly for b2 and b3
Now I want that on Click开发者_运维技巧 event I store the title in the string. How i can achieve it?
In Other Words: What function/method would I have to implement in my View Controller class to handle a click event on a UIButton?
You can make an IBAction
for touch up inside in Interface Builder.Then I am assuming you have a UITextField for the text.
Since your not using Interface Builder you need to:
-(void)getStringFromText:(id)sender {
NSString *input = sender.titleLabel;
}
//In some start up method.
[b1 addTarget: target action: @selector(getStringFromText:) forControlEvents: UIControlEventTouchUpInside];
When you declare your UIButtons, you need to put "IBOutlet" before them in your class definition - then you need to to use Interface Builder to link/reference the UIButtons to the ones you have declared in your code.
Once you have done this, you need to develop a (IBAction) method - and have it simply get the "text" property from the calling object. In Interface Builder reference this method for all the buttons for the "touch up" event.
If this isn't clear I will post up code as an example?
精彩评论