当您在弹出框内进行不同操作时,您必须使用委托才能查看主视图中的更改。首先,您需要在 popover 控制器头文件中创建一个协议:
#import <Foundation/Foundation.h>
@class MyPopoverController;
@protocol MyPopoverDelegate
- (void)valueChanged:(NSString*) newVal;
@end
@interface MyPopoverController: UIPopoverController
@property (weak) id<MyPopoverDelegate> delegate;
@end
然后在 .m 你这样实现它:
- (void) someActionOccured
{
if([self.delegate respondsToSelector:@selector(valueChanged:)]){
[self.delegate valueChanged:valueYouWantToSendBack];
}
}
请记住,在您的主类中,您必须实现MyPopoverDelegate
协议:
@interface MainViewController: UIViewController <MyPopoverDelegate>
当你实例化你的 popover 控制器时:
/*
** inside MainViewController.m
*/
// remember to assign it's delegate
MyPopoverController *popoverController = [MyPopoverController alloc] init];
popoverController.delegate = self;
此外,您必须实现协议的方法:
/*
** inside MainViewController.m
*/
- (void)valueChanged:(NSString*) newVal
{
// process the string and display it where you need it
}