我在这方面完全一无所知!逐步解释可赚取奖励积分。
最佳答案
在 iOS 中我们有 UIButtons(不是 NSButtons)
以下是如何以编程方式创建 UIButton:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //3
[button setFrame:CGRectMake(320, 0, 100,100 )];
[button setTitle:@"PressMe!" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];//2
[aView addSubview:button]; //1
和:
- (void)buttonPressed:(UIButton *)sender{
//do something here ;)
[sender setTitle:@"PressMe!. Again" forState:UIControlStateNormal];
}
1:如果您位于 UIViewController
中,则 aView
将是 self.view
如果您在其他地方,只需传递包含您的按钮的 View
2:最简单的情况就是这样,但您可以将任何对象设置为目标。该对象必须具有您在 @selector(...)
中传递的方法。否则,按下按钮时您的应用程序将崩溃。
关于Objective-C 在运行时向我的 iPhone 应用程序添加 NSButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4688826/