iphone - 在 iPhone 中的两个 View Controller 之间使用委托(delegate)

标签 iphone ios delegates protocols

我有两个 View Controller : View Controller 和viewcontroller2nd。我在其中之一中有 UILabel,并希望在单击 viewcontroller2nd 中的按钮(名为 Go)时更改它。我正在使用委托(delegate)和协议(protocol)来做到这一点。

代码如下所示:

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

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

最佳答案

看来您从未设置过委托(delegate)。 您可以在 passData 方法中执行此操作:

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

关于iphone - 在 iPhone 中的两个 View Controller 之间使用委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13027206/

相关文章:

iphone - 程序在使用 NSLOG 以外的变量时收到 SIGABRT

iOS Rich Push notifications with Xcode, Swift3 但无法获取图像

ios - 为什么委托(delegate)方法可以在不声明协议(protocol)一致性的情况下工作?

c# - Lambda 委托(delegate)决议

iphone - 由于未捕获的异常,xcode 终止应用程序

iphone - 给定文件和目录生成相对路径的 Objective-C 代码

iphone - UISegmentedControl、UIToolbar 和 UINavigationItem

ios - 我可以在自定义 View 中直接将约束设置为 super View 吗?

ios - 在 Xcode 中调整 Pane 大小

swift - 从 tableView 中删除 StructItem