以编程方式手动链接XIB中声明的UIButton和VIewController中的属性



XCode要求您声明像IBActionIBOutlet这样的东西,然后您需要使用MOUSE来连接引用出口,这是我非常讨厌的事情。

我想以编程方式连接XIB中声明的项,这可能吗?

可能您也应该尝试以编程方式添加按钮。示例:

- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect myButtonFrame = CGRectMake(70, 200, 180, 60);
[myButton addTarget:self action:@selector(MyButtonAction)forControlEvents:UIControlEventTouchUpInside];
myButton.frame = myButtonFrame;
[self.view addSubview:myButton];

}

-(void)MyButtonAction
{
NSLog(@"ACTION");
}

是的,这是可能的。但不推荐使用。您可以将标签分配给控件,然后使用[self.view viewWithTag:TAG];消息。或者您可以在self.view.subviews数组中查找所需的控件。但不管怎样,你最好不要那样做。

最新更新