子界面
@protocol getLabelDelegate <NSObject>
- (void)getNString:(NSString *)str;
@end
@interface AddViewController : UIViewController
@property (weak, nonatomic) id <getLabelDelegate> delegate;
@end- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(finish)];
self.navigationItem.rightBarButtonItem = rightBtnItem;
}
-(void) finish
{
[self.delegate getNString:@"abc"];
[self.navigationController popViewControllerAnimated:YES];
}主界面
@interface MyViewController : UIViewController<getLabelDelegate>
@property (weak, nonatomic) id<getLabelDelegate> delegate;
@end@implementation MyViewController
{
// NSMutableArray *myArray;
NSMutableArray *testArr;
}
- (void)getNString:(NSString *)str
{
[testArr addObject:[[NSString alloc] initWithString:str]];
}- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
self.navigationItem.rightBarButtonItem = rightBtnItem;
}
-(void)add
{
AddViewController *addvc = [[AddViewController alloc] init];
addvc.delegate = self;
[self.navigationController pushViewController:addvc animated:YES];
}
本文介绍了一个iOS应用中实现子界面与主界面通信的例子。通过定义协议`getLabelDelegate`并使用代理模式,子界面`AddViewController`能够将字符串传递给主界面`MyViewController`,主界面则负责收集这些字符串到数组中。此方案展示了如何在iOS应用的不同视图控制器间进行简单的数据交互。
582

被折叠的 条评论
为什么被折叠?



