您的第一个控制器应该在实例化第二个控制器时,将第二个的委托设置为指向第一个视图控制器。因此,您的第一个视图控制器可能如下所示:
// FirstViewController.h
#import <UIKit/UIKit.h>
@protocol FirstViewControllerDelegate <NSObject>
- (void)dealWithButton;
@end
@interface FirstViewController : UIViewController <FirstViewControllerDelegate>
@end
使用如下实现:
// FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation FirstViewController
- (IBAction)goToNextViewController:(id)sender
{
SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
secondController.delegate = self;
[self.navigationController pushViewController:secondController animated:YES];
}
- (void)dealWithButton
{
NSLog(@"Dealt with button from second controller");
}
@end
您的第二个控制器可能如下所示:
// SecondViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@class FirstViewController;
@interface SecondViewController : UIViewController
@property (weak, nonatomic) id<FirstViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)buttonPressed:(id)sender;
@end
使用如下实现:
// SecondViewController.m
#import "SecondViewController.h"
@implementation SecondViewController
@synthesize delegate = _delegate;
@synthesize label = _label;
- (IBAction)buttonPressed:(id)sender
{
int random_num;
random_num = (arc4random() % 5 - 1) + 1;
if (random_num == 1)
self.label.text = @"hello1";
else if (random_num == 2)
self.label.text = @"hello2";
else if (random_num == 3)
self.label.text = @"hello3";
else if (random_num == 4)
self.label.text = @"hello4";
[self.delegate dealWithButton];
}
@end
更新:
您最初的问题没有明确说明您是否希望标签位于第一个控制器或第二个控制器上。我上面的回答假设您希望在第二个控制器上使用它,但回想起来,您可能希望在第一个控制器(委托)上使用它。如果是这样,下面的代码就是这样做的。请注意,我不只是在 中更新第一个视图控制器的标签dealWithButton
,因为这很危险,因为您不知道视图是否可见(如果您收到 ,则可能已卸载didReceiveMemoryWarning
)。所以我等待viewWillAppear
。同样,第一个视图控制器:
// FirstViewController.h
#import <UIKit/UIKit.h>
@protocol FirstViewControllerDelegate <NSObject>
- (void)dealWithButton;
@end
@interface FirstViewController : UIViewController <FirstViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
及其实现:
// FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
{
NSString *_labelText;
}
@end
@implementation FirstViewController
@synthesize label = _label;
// if you're using storyboards, it would be like:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"delegateSegue"])
{
SecondViewController *destinationController = segue.destinationViewController;
FirstViewController *sourceController = segue.sourceViewController;
destinationController.delegate = sourceController;
}
}
// if not using storyboards, you probably have a button like:
- (IBAction)goToNextViewController:(id)sender
{
SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
secondController.delegate = self;
[self.navigationController pushViewController:secondController animated:YES];
}
- (void)dealWithButton
{
// note, because this is being called by the second view controller, you should *not* update the UI
// directly, because you can't be assured this view controller's view is still in memory (if you got
// a didReceiveMemoryWarning while on the second view controller, this first view controller will
// stay in memory, but its view could have been released). So save what you want the label to be,
// and update it on viewWillAppear (and if the view was released, it will be reloaded by the time
// you hit viewWillAppear.
//
// clearly, if you were doing view controller containment and this was the parent view, you wouldn't
// want to do this. But I assume you're dealing with a simple push/present view controller situation.
int random_num;
random_num = (arc4random() % 5 - 1) + 1;
if (random_num == 1)
_labelText = @"hello1";
else if (random_num == 2)
_labelText = @"hello2";
else if (random_num == 3)
_labelText = @"hello3";
else if (random_num == 4)
_labelText = @"hello4";
NSLog(@"Dealt with button from second controller");
}
- (void)viewWillAppear:(BOOL)animated
{
self.label.text = _labelText;
}
@end
第二个视图控制器:
// SecondViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@class FirstViewController;
@interface SecondViewController : UIViewController
@property (weak, nonatomic) id<FirstViewControllerDelegate> delegate;
- (IBAction)buttonPressed:(id)sender;
@end
及其实现:
// SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize delegate = _delegate;
- (IBAction)buttonPressed:(id)sender
{
[self.delegate dealWithButton];
}
@end