2012-10-23 30 views
2

我有两个视图控制器:视图控制器和viewcontroller2nd。我在其中的一个UILabel中,并且想要在viewcontroller2nd中的按钮(名为Go)被点击时更改它。我正在使用代表和协议来完成它。在两个视图控制器之间使用代理iphone

的代码看起来是这样的:

ViewController.h

#import <UIKit/UIKit.h> 
#import "ViewController2nd.h" 

@interface ViewController : UIViewController <SecondViewControllerDelegate> 
{ 
    IBOutlet UILabel *lbl; 
    ViewController2nd *secondview; 

} 
-(IBAction)passdata:(id)sender; 

@end 

ViewController.m

#import "ViewController.h" 
#import "ViewController2nd.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(void) changeLabel:(NSString*)str{ 
    lbl.text = str; 
} 

-(IBAction)passdata:(id)sender{ 
    ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil]; 
    [self presentViewController:second animated:YES completion:NULL]; 
} 

@end 

Viewcontroller2nd.h

#import <UIKit/UIKit.h> 


@protocol SecondViewControllerDelegate <NSObject> 
@optional 
-(void) changeLabel:(NSString*)str; 
@end 

@interface ViewController2nd : UIViewController{ 

    IBOutlet UIButton *bttn; 
    id <SecondViewControllerDelegate> delegate; 

} 

@property (retain) id delegate; 

-(IBAction)bttnclicked; 
-(IBAction)back:(id)sender; 
@end 

ViewController2nd.m

#import "ViewController2nd.h" 
#import "ViewController.h" 

@interface ViewController2nd() 

@end 

@implementation ViewController2nd 

@synthesize delegate; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(IBAction)bttnclicked{ 
    [[self delegate] changeLabel:@"Hello"]; 
} 

-(IBAction)back:(id)sender{ 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

@end 

两个视图之间的控件传递工作正常。但是,当我点击viewcontroller2nd中的go按钮时,它不会将标签的值更改为Hello。代码有什么问题?需要一些指导。

+0

您是否检查了第一个视图控制器的changeLabel被调用?尝试把NSLog,并确认它正在调用。 – applefreak

+0

它没有被调用... – lakesh

+0

嗯,这是因为你没有通过委托给第二个控制器。此外,代表永远不会被保留,否则你有保留周期内存的问题。您应该将其声明为在第二个控制器中分配。 – applefreak

回答

2

看来你从来没有设置委托。 你可以在你的passData方法:

-(IBAction)passdata:(id)sender{ 
    ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil]; 
    second.delegate = self; 
    [self presentViewController:second animated:YES completion:NULL]; 
} 
+0

它应该是这个还是自己? – lakesh

+0

仍然我不能改变标签的价值... – lakesh

+0

自我,对不起我在csharp同时工作,所以我有点困惑^^ – Ashbay

1

嗯,尝试在changeLabel委托方法如下:

-(void) changeLabel:(NSString*)str{ 
    lbl.text = str; 
    [lbl setNeedsDisplay]; 
} 

编辑:

除此之外: 你的标签是IBOutlet,对吧?你的标签连接与IBOutlet中物业(即LBL)的厦门国际银行正确与文件的所有者界面生成器

+0

仍然我不能改变标签的价值... – lakesh

+0

你的标签是一个IBOutlet,对不对?您是否已在文件所有者的界面生成器中将xib中的标签与IBOutlet属性(即lbl)正确连接? –

+0

是的..我没有在界面生成器中正确连接属性...仍然无法改变它... – lakesh

1

如果你想通过使用委托这样做,则更改passdata像:

-(IBAction)passdata:(id)sender 
    { 
     ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil]; 
     [second setDelegate:self]; 
     [self presentViewController:second animated:YES completion:NULL]; 
    } 

你没有代表就可以做到这一点。 在secondView只需创建视图控制器的一个对象,并合成它想:

#import "ViewController.h" 
@interface ViewController2nd : UIViewController 
{ 

    IBOutlet UIButton *bttn; 
    ViewController *parent; 

} 

@property (nonatomic, assign) ViewController *parent; 

-(IBAction)bttnclicked; 
-(IBAction)back:(id)sender; 
@end 

,并在passdata改变它像

-(IBAction)passdata:(id)sender 
{ 
    ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil]; 
    [second setParent:self]; 
    [self presentViewController:second animated:YES completion:NULL]; 
} 

,改变bttnclicked这样的:

-(IBAction)bttnclicked 
{ 
    [parent changeLabel:@"Hello"]; 
} 
+0

我告诉我使用我的资深代表..那就是为什么..我没有选择... – lakesh

+0

我编辑了我的答案,请在点击的按钮上添加nslog并更改标签方法。 –

1

我写的跟随你!

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UILabel *label; 

@end 

#import "ViewController.h" 
#import "ViewController2.h" 

@interface ViewController() <ViewController2Delegate> 

@end 

@implementation ViewController 

-(void)passdata:(NSString *)data 
{ 
    self.label.text = data; 
} 

- (IBAction)buttonClicked:(id)sender { 
    ViewController2 *v = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; 
    v.delegate = self; 
    [self presentViewController:v animated:YES completion:nil]; 
} 

@end 

#import <UIKit/UIKit.h> 

@protocol ViewController2Delegate <NSObject> 

- (void)passdata:(NSString*)data; 

@end 

@interface ViewController2 : UIViewController 

@property(assign, nonatomic) id<ViewController2Delegate> delegate; 

@end 

#import "ViewController2.h" 

@interface ViewController2() 

@end 

@implementation ViewController2 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (IBAction)buttonClicked:(id)sender { 
    [self.delegate passdata:@"hello"]; 
} 

- (IBAction)backButtonClicked:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

@end 
0
  • 你并不需要导入头“ViewController2nd.h”在ViewController.h和ViewController.m
  • 双方你申报的UILabel没有财产一样,@property(强,非原子)。
  • 可能是,您没有正确地将UILabel映射到您的.storyboard或.xib文件。
  • 你忘了分配第二视图控制器的委托,同时调用控制器

    ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil]; 
    second.delegate = self; 
    [self presentViewController:second animated:YES completion:NULL]; 
    

如果有人用故事板的viewcontrollers导航。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    { 
     if ([segue.identifier hasPrefix:@"view_to_capture"]) { 
      ViewController2nd *destination = segue.destinationViewController; 
      destination.delegate = self; 
     } 
    } 
相关问题