我有两个ViewControllers:A和B,
我在A中计算B的变量,然后通过创建ViewController B的对象来推动B的变量值.B的变量本身是声明的。现在我在ViewController B中更改变量的值并返回到A.我想在A中使用变量的变量值。我已经尝试了协议,但在A中我得到该变量的0值。 这是我的代码:
ViewController A.h
#import <UIKit/UIKit.h>
#import "B.h"
@class B;
@interface ViewController : UIViewController<nxtDelegate>
{
int vcheck;
}
@property(nonatomic,assign) int vcheck;
实施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 B.h
#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 B.m
- (IBAction)chngAction:(id)sender {
[_delegate chngvalue:chcek];
NSLog(@"%@",_delegate);}
答案 0 :(得分:1)
我不知道为什么它不起作用,但我只是按照下面的说法进行操作
@property(nonatomic, assign)id<nxtDelegate> Delegate;
合成属性名称Delegate
@synthesize Delegate;
并且在推送期间我设置了委托
ViewControllerB *ViewControllerBObj=[[ViewControllerB alloc] init];
[ViewControllerBObj setDelegate:self];
[[self navigationController] pushViewController:ViewControllerBObj animated:YES];
在IBAction中
- (IBAction)chngAction:(id)sender
{
if([[self Delegate] respondsToSelector:@selector(chngvalue:)])
[[self Delegate] chngvalue:chcek];
}