ios開發(fā)逆向傳值的幾種方法整理

第一種:代理傳值

第二個控制器:

@protocol WJSecondViewControllerDelegate <NSObject>
- (void)changeText:(NSString*)text;
@end
 @property(nonatomic,assign)id<WJSecondViewControllerDelegate>delegate;
 
- (IBAction)buttonClick:(UIButton*)sender {
_str = sender.titleLabel.text;
[self.delegate changeText:sender.titleLabel.text];
[self.navigationController popViewControllerAnimated:YES];
}

第一個控制器:

- (IBAction)pushToSecond:(id)sender {
WJSecondViewController *svc = [[WJSecondViewController alloc]initWithNibName:@"WJSecondViewController" bundle:nil];
svc.delegate = self;
svc.str = self.navigationItem.title;
[self.navigationController pushViewController:svc animated:YES];
[svc release];
}
- (void)changeText:(NSString *)text{
self.navigationItem.title = text;
}

第二種:通知傳值

第一個控制器:


 //注冊監(jiān)聽通知
 [[NSNotificationCenter defaultCenter] addObserver:self         selector:@selector(limitDataForModel:) name:@"NOV" object:nil];
- (void)limitDataForModel:(NSNotification *)noti{
self.gamesInfoArray = noti.object;
}

第二個控制器:

//發(fā)送通知
  [[NSNotificationCenter defaultCenter]     postNotificationName:@"NOV" object:gameArray];

第三種:單例傳值

Single是一個單例類,并且有一個字符串類型的屬性titleName

在第二個控制器:

- (IBAction)buttonClick:(UIButton*)sender {
Single *single = [Single sharedSingle];
single.titleName = sender.titleLabel.text;
[self.navigationController popViewControllerAnimated:YES];
}

第一個控制器:

- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
Single *single = [Single sharedSingle];
self.navigationItem.title = single.titleName;
}

第四種:block傳值

第二個控制器:
@property (nonatomic,copy) void (^changeText_block)(NSString*);

  • (IBAction)buttonClick:(UIButton*)sender {
    _str = sender.titleLabel.text;
    self.changeText_block(sender.titleLabel.text);
    [self.navigationController popViewControllerAnimated:YES];
    }
第一個控制器:
  • (IBAction)pushToSecond:(id)sender {
    WJSecondViewController *svc = [[WJSecondViewController alloc]initWithNibName:@"WJSecondViewController" bundle:nil];
    svc.str = self.navigationItem.title;
    [svc setChangeText_block:^(NSString *str) {

    self.navigationItem.title = str;
    }];
    [self.navigationController pushViewController:svc animated:YES];
    }

第五種:extern傳值

第二個控制器:

extern NSString *btn;

  • (IBAction)buttonClick:(UIButton*)sender {
    btn = sender.titleLabel.text;
    [self.navigationController popViewControllerAnimated:YES];
    }
第一個控制器:

NSString *btn = nil;

  • (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.title = btn;
    }
第六種:KVO傳值

第一個控制器:
  • (void)viewDidLoad {
    [super viewDidLoad];
    _vc =[[SecondViewController alloc]init];
    //self監(jiān)聽vc里的textValue屬性
    [_vc addObserver:self forKeyPath:@"textValue" options:0 context:nil];
    }
第二個控制器:
  • (IBAction)buttonClicked:(id)sender {
    self.textValue = self.textField.text;
    [self.navigationController popViewControllerAnimated:YES];
    }

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容