在iphone中使用两个viewcontrollers之间的委托

时间:2012-10-23 09:21:13

标签: iphone ios delegates protocols

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

代码如下所示:

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。代码有什么问题?需要一些指导。

5 个答案:

答案 0 :(得分:2)

似乎你从未设置过委托。 您可以在passData方法中执行此操作:

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

答案 1 :(得分:1)

嗯,请在您的changeLabel委托方法中尝试以下操作:

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

编辑:

除此之外: 你的标签是IBOutlet,对吧?您是否在xib中将标签与IBOutlet属性(即lbl)正确地连接到文件所有者的界面构建器

答案 2 :(得分: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中创建一个ViewController对象,然后将其合成为:

#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"];
}

答案 3 :(得分: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

答案 4 :(得分:0)

  • 您无需在ViewController.h和ViewController.m中导入标题“ViewController2nd.h”
  • 你声明没有属性的UILabel,如@property(强,非原子)。
  • 可能是,您没有将UILabel正确映射到.storyboard或.xib文件。
  • 您忘记在调用控制器时分配第二个视图控制器委托

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

如果有人使用storyboard进行viewcontrollers导航。

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