简体   繁体   English

如何使用协议在两个视图控制器之间传递值

[英]How to use Protocol to passvalue between two viewcontrollers

I have two ViewControllers: A and B, 我有两个ViewController:A和B,

I am computing variable of B in A and then pushing B with its variable value by creating object of ViewController B. Variable of B is declare in itself. 我正在计算A中B的变量,然后通过创建ViewController B的对象将B的变量值推入。B的变量本身就是声明的。 Now I am changing value of variable in ViewController B and return back to the A. I want to use Changed value of variable in A. I have tried protocol for this but in AI am getting 0 value of that variable. 现在,我在ViewController B中更改变量的值,然后返回到A。我想在A中使用变量的Changed值。我已经尝试过协议,但在AI中该变量的值为0。 Here is my code: 这是我的代码:

ViewController Ah ViewController啊

#import <UIKit/UIKit.h>
#import "B.h"
@class B;
@interface ViewController : UIViewController<nxtDelegate>
{
int vcheck;
 }
@property(nonatomic,assign) int vcheck;

Implementation of A: A的实现:

#import "ViewController.h"

@interface ViewController ()

 @end

@implementation ViewController
@synthesize lbl,txt,vcheck;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

-(void)viewWillAppear:(BOOL)animated{
NSLog(@"vcheckis:%d",vcheck);

}
-(void)chngvalue:(int)i{
vcheck=i;
}

ViewController Bh ViewController Bh

#import <UIKit/UIKit.h>

@protocol nxtDelegate <NSObject>

-(void)chngvalue:(int )i;

@end
@interface nextviewController :   UIViewController<UITableViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
{
int chcek;
}
@property(weak,nonatomic)id<nxtDelegate> delegate;
- (IBAction)btnclick:(id)sender;
@property(nonatomic,assign) int chcek;

ViewController Bm ViewController Bm

- (IBAction)chngAction:(id)sender {
[_delegate chngvalue:chcek];
NSLog(@"%@",_delegate);}

I don't know why it's not working but I just do the below and it's work fine for me 我不知道为什么它不起作用,但是我只做下面的事情,对我来说很好

@property(nonatomic, assign)id<nxtDelegate> Delegate;

synthesize the property name Delegate 合成属性名称委托

@synthesize Delegate;

and during push I set the delegate 在推送过程中,我设置了代表

ViewControllerB *ViewControllerBObj=[[ViewControllerB alloc] init];
[ViewControllerBObj setDelegate:self];
[[self navigationController] pushViewController:ViewControllerBObj animated:YES];

In IBAction 在IBAction中

- (IBAction)chngAction:(id)sender 
{
    if([[self Delegate] respondsToSelector:@selector(chngvalue:)])
      [[self Delegate] chngvalue:chcek];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个视图控制器的委托协议 - delegate protocol for two viewcontrollers 如何使用 iOS swift hero 框架在两个视图控制器之间进行转换? - How to use iOS swift hero framework for transition between two viewcontrollers? iOS Swift,在协议中有一个通用方法,在两个ViewController中有两个实现,如何确定调用哪个ViewControllers方法? - iOS Swift, with one common method in protocol and two implementations in two ViewControllers, how to decide which ViewControllers method is invoked? 如何使用UIPickerview在两个ViewController之间导航 - How to navigate between two viewcontrollers using a UIPickerview 如何在iwatch中的两个viewcontroller之间传递数据? - How to pass data between two viewcontrollers in iwatch? 如何在两个ViewController之间传递数据 - How to pass data between two viewcontrollers 如何让两个 ViewController 使用一个 NSObject - how to let two ViewControllers use one NSObject 如何在两个ViewController之间的Swift App中传递JSON数据 - How to pass json data in swift app between two viewcontrollers swift - 如何在一个 tabBarItem 的两个视图控制器之间切换,并自定义它? - swift - how switch between two viewcontrollers from one tabBarItem , and customize it? 如何在两个ViewController之间链接数组? - How do I link an array between two ViewControllers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM