在我的项目中,有两个 View Controller - 假设是 firstViewController 和 secondViewController。第二个 View Controller 有一个按钮,我想确保当按钮被按下时,第二个 View Controller 以某种方式告诉第一个 View Controller ——“嘿,我被按下了,做点什么!”,它会做点什么,就像改变标签一样。这怎么可能执行?提前致谢。一些代码:
@interface firstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@end
@implementation firstViewController
@synthesize textLabel;
@end
@interface secondViewController : UIViewController
-(IBAction)buttonPressed;
@end
@implementation secondViewController : UIViewController
-(IBAction)buttonPressed{
// Hey, I got pressed! Set the text on textLabel to "OK"!
}
@end
最佳答案
这是一个非常简单的objective-c的委托(delegate)和协议(protocol)机制的案例。 看看这个tutorial这将向您解释它是如何完成的……您也可以通过通知来做到这一点,但通常不建议这样做……(因为通常在接收者未知时使用通知,就像在 UIDeviceBatteryLevelDidChangeNotification 的情况下,您不完全知道哪个 View Controller 想知道这件事。)
关于objective-c - 从另一个 View Controller 修改 View Controller 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9751329/