Call NSObject type file with click event of button
I want to create a class of NSObject type and then I want to create one button click here in this file and also perform some ev开发者_高级运维ent with this button. After that, I want to call this file in my view with the click event of another button called "push me". What code should I write to call that NSObject type file?
Assuming you have a ViewController with a nib file and the Custom Class(derived from NSObject) is named as MyClass. And in your custom class you have created and named the button as myButton. And myButton is defines as a property in the Custom Class. You can write a method which will be called on the "push me" button click. The code can look something like this.
-(IBAction)pushMeButtonClicked
{
MyClass *myObj=[[MyClass alloc] init];
[self.view addSubview:myObj.myButton];
}
The init method of the MyClass can be defined as:
-(id)init
{
self=[super init];
if(self)
{
myButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
}
}
And of course you need to set the frame for the button myButton as per your needs.
精彩评论