【问题标题】:how to call a method from one controller to another controller in Objective-C如何在 Objective-C 中将方法从一个控制器调用到另一个控制器
【发布时间】:2018-05-19 20:27:56
【问题描述】:

大家好,我是Mac OS Developer 的初学者。在名为 Controller1 的控制器中调用方法时遇到问题。

这是我的 Controller1 代码:

Controller1.h

#import <Cocoa/Cocoa.h>

@interface Controller1 : NSViewController 

- (void)LoadViewActiveCall;

@end

Controller1.m

#import "Controller1.h"
@implementation Controller1

- (void)showCallViewController:(NSViewController*)target_viewcontroller2{
    NSLog(@"Run method showCallViewController");
    NSLog(@"Parameter targe view controller 2 : %@",target_viewcontroller2);
    if(current_controller2!=nil){
        [current_controller2.view removeFromSuperview];
        [current_controller2 removeFromParentViewController];
    }
    [self addChildViewController:target_viewcontroller2];
    target_viewcontroller2.view.frame=container_Call.frame;
    [container_Call addSubview:target_viewcontroller2.view];
    current_controller2=target_viewcontroller2;
}

- (void)LoadViewActiveCall {
    [self showCallViewController:vCall];
    NSLog(@"keluar active call berhasil");
}

@end

我想在Controller2的按钮IBAction上调用LoadViewActiveCall方法

Controller2.h

#import <Cocoa/Cocoa.h>
#import "Controller1.h"

@interface Controller2 : NSViewController

- (IBAction)audioCall:(id)sender;

@end

Controller2.m

#import "Controller1.h"
#import "Controller2.h"

@implementation Controller2

- (IBAction)audioCall:(id)sender {
    Controller1 *co1 = [[Controller1 alloc] init];
    [co1 LoadViewActiveCall];
}

@end

我的问题是当我按下 audioCall 按钮时,但没有调用 LoadViewActiveCall 方法。

对不起,我的英语不好

【问题讨论】:

  • audioCall: 方法被调用了吗?如果是这样,[[Controller1 alloc] init] 返回什么?你有没有在调试器中通过audioCall: 看看发生了什么?
  • 是的,audioCall: 被调用,[[Controller1 alloc] init] 返回一个对象(如果我错了,请纠正我)。什么都没有发生,只是空白还是我做错了?
  • 您在控制器中放入了哪些视图?此外,您还有一个名为target_viewcontroller2 的第三个视图控制器。那是什么?你也从来没有在[-Controller1 showCallViewController:]中安装self
  • 我尝试将视图加载到容器视图中,在这种情况下是container_calltarget_viewcontroller2 是一个参数参考vCall=[self.storyboard instantiateControllerWithIdentifier:@"activeAudioCallWindow"];.... 如果调用LoadViewActiveCall 方法的按钮在同一个控制器中它是有效的,但是如果按钮在不同的控制器中,LoadViewActiveCall 方法不会被调用。 如果您有困惑,请见谅

标签: objective-c xcode macos nsviewcontroller


【解决方案1】:

在您的 FirstViewController 中初始化 SecondViewController。

let vc = self.storyboard?.instantiateViewController(withIdentifier:
"SecondViewController") as! SecondViewController

在SecondViewController头文件(SecondViewController.h)中添加你要调用的方法

@interface SecondViewController : NSViewController

- (void) LoadViewActiveCall;

然后从 FirstViewController 调用

let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController

[vc LoadViewActiveCall];

【讨论】:

    【解决方案2】:

    添加 NSNotificationCenter 以调用方法。

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioCall:) name:@"notificationName" object:nil];
    
    -(void) audioCall:(NSNotification*)notification
    {
    }
    

    从第二个视图控制器发布通知,如下所示。

    [[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName" object:obj];
    

    【讨论】:

      【解决方案3】:

      您从另一个视图控制器调用函数的方式是正确的。

      请检查您是否已将按钮操作正确连接到功能

      - (IBAction)audioCall:(id)sender
      

      请检查并回复。它非常适合使用 UIViewcontroller 的 ios 单视图应用程序

      【讨论】:

      • 是的,连接到函数的按钮动作是正确的。
      • 我已经检查了 uiviewcontroller。该功能运行良好
      猜你喜欢
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多