2012-10-23 53 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; 
     } 
    } 
相關問題