如何在没有StoryBoard的两个ViewController之间使用委托?

时间:2018-02-05 15:35:46

标签: ios objective-c delegates protocols viewcontroller

我想从viewController触发另一个viewController的消息。 我编码如下,但viewController没有调用委托。 我想在没有stroyboard的情况下给代表打电话。我只想向另一个viewController发送消息。

viewController2.h

@protocol ViewController2Delegate;

@interface ViewController2 : UIViewController
@property (nonatomic, weak) id<ViewController2Delegate> delegate;
@end

@protocol ViewController2Delegate <NSObject>
- (void)showSomethingByDelegate;
@end

viewController2.m

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)buttonPressed:(id)sender {
    [self.delegate showSomethingByDelegate];
}

@end

viewController.m

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

@interface ViewController ()
<ViewController2Delegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    ViewController2 *vc2 = [[ViewController2 alloc] init];
    vc2.delegate = self;

}
// Below method was not called by delegate.
- (void)showSomethingByDelegate {
    NSLog(@"Button Was Pressed!!!");
}


@end

2 个答案:

答案 0 :(得分:0)

ViewController2 *vc2 = [[ViewController2 alloc] init];

这行代码没有按照您的想法进行,这是创建ViewController2的新实例并将其存储在变量vc2中。在任何时候(基于您的代码)都会将此变量添加到导航堆栈并显示在屏幕上。


修改
同样如Holex所注意到的,vc2变量将在viewDidLoad完成后从内存中删除,因为您没有保留它的引用。您可能需要创建一个属性来保留它,或者将其推送到导航堆栈以使navigationController保持在其上。

有多种方法可以解决此问题:

  1. 不是创建新的viewController2,而是找到对屏幕上已显示的引用的引用(如果它在屏幕上)。
  2. 如果它不在导航堆栈中,请将vc2添加到导航堆栈[self.navigationController pushViewController:vc2 animated:YES];(假设您有导航控制器)。
  3. 如果不在堆栈上,并且不打算使用navigationController,则以模态方式显示vc2,例如[self presentViewController:vc2 animated:YES completion:nil];

答案 1 :(得分:0)

让我举一个更简单的例子。

文件:firstVC.h

/* This define the protocol object,
 you can write methods required or optional the diference is when
 the protocol is used in a class, xcode show you a yellow warning with required methods
 like UITableViewDelegate, UITextFieldDelegate... */

@protocol firstVCDelegate <NSObject>

@required
- (void)didMessageFromOtherViewController: (NSString *)messageStr;

@optional
- (void)didOtherMessageNotRequired: (NSString *)messageStr;

@end

/* This is the definition of first UIViewController */

@interface firstViewController : UIViewController
@end


/* This is the definition of second UIViewController object with
 a property that is our protocol 'firstVCDelegate' */

@interface secondViewController : UIViewController

@property (nonatomic, weak) id <firstVCDelegate> firstVCDelegate;

- (void)executeDelegateProcess;

@end

档案:firstVC.m

#import "firstVC.h"

#pragma mark - First UIViewController with delegate

@interface firstViewController() <firstVCDelegate>
@end

@implementation firstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Creating the 'secondViewController' object with delegate on this class
    secondViewController *svc = [secondViewController new];

    // Assign the delegate class
    [svc setFirstVCDelegate: self];

    // Run the delegate logic
    [svc executeDelegateProcess];
}

- (void)didMessageFromOtherViewController:(NSString *)messageStr
{
    // Receiving the message from the instance of our protocol in the 'secondViewController' class
    NSLog(@"MESSAGE #1: %@", messageStr);
}

- (void)didOtherMessageNotRequired:(NSString *)messageStr
{
    // Receiving the message in optional method
    NSLog(@"MESSAGE #2: %@", messageStr);
}

@end

#pragma mark - Second UIViewController

@implementation secondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)setFirstVCDelegate:(id<firstVCDelegate>)firstVCDelegate
{
    if (firstVCDelegate)
        _firstVCDelegate = firstVCDelegate;
}

- (void)executeDelegateProcess
{
    // This method is only for demo
    // You can execute your delegate in the way you need to use

    if (_firstVCDelegate) {
        [_firstVCDelegate didMessageFromOtherViewController: @"Hello world, using the required method from secondViewController class"];

        [_firstVCDelegate didOtherMessageNotRequired: @"Hello code using the optional method"];
    }
}

@end

在方法 didFinishLaunchingWithOptions appDelegate.m 中,你可以放置它,你需要 #import“firstVC.h” < / p>

self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
        self.window.autoresizesSubviews = YES;
        [self.window makeKeyAndVisible];

        [_window setRootViewController: [firstViewController new]];

执行并查看两条日志消息,希望我帮助过:)