当控制器关闭时如何在另一个视图中调用方法

发布于 2024-11-03 02:50:01 字数 1075 浏览 1 评论 0原文

基本上我遇到的问题是我无法从另一个称为表的视图调用主视图控制器(称为记录器)中的方法。

当用户单击按钮时,表视图作为 Modalview 控制器(presentmodalViewController)从 Recorder 加载。表视图允许我的用户从设置 1 更改为设置 2,并有一个完成按钮(调用解雇模态视图控制器)并将用户返回到主视图(记录器)。

我想要的是当单击表视图上的完成按钮时调用记录器中的方法。此方法称为 Changeview,用于更改设置。我目前无法正确调用此方法。

我当前的代码是:

changeView 方法

- (void)changeView
{

    [levelsView changeView];

}

TableViewController 接口文件

RecorderViewController*recorderViewController;

@property (nonatomic, retain) RecorderViewController *recorderViewController;

TableViewController 实现文件

@synthesize recorderViewController;

    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        [recorderViewController changeView];

    }

按下“完成”按钮时调用的方法

- (IBAction) Switch: (id) sender { 

 [self dismissModalViewControllerAnimated:YES];

}

当前的代码不会给我任何错误,但它也不会更改设置。我也尝试过设置通知,但没有成功。对此有何见解?

Basically the problem I am having is I am unable to call a method in my Main view controller(called Recorder) from another view called Table.

The table view is loaded as a Modalview controller(presentmodalViewController) from Recorder when the user clicks on a button. The table view lets my users change from setting 1 to setting 2 and has a done button (which calls dismissmodalviewcontroller) and returns the user to the main view(Recorder).

What I want is to call a method in Recorder when the done button is clicked on Table View. This method is called Changeview and changes the setting. I am currently unable to call this method properly.

The current code I have is:

changeView method

- (void)changeView
{

    [levelsView changeView];

}

TableViewController interface file

RecorderViewController*recorderViewController;

@property (nonatomic, retain) RecorderViewController *recorderViewController;

TableViewController implementation file

@synthesize recorderViewController;

    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        [recorderViewController changeView];

    }

Method called when Done button is pressed

- (IBAction) Switch: (id) sender { 

 [self dismissModalViewControllerAnimated:YES];

}

The current code does not give me any errors but it also does not change the setting. I have also tried to setup notifications with no luck. Any insight into this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春夜浅 2024-11-10 02:50:01

初始化 tableViewController 时是否设置 recorderViewController 的值(即,是否在 Recorder 类中设置 tableViewController.recorderViewController = self )?如果没有,那么您对 ​​[recorderViewController changeView] 的调用将向 nil 发送一条消息 – 这不会崩溃,但也不会执行任何操作。

顺便说一句,将 TableViewController 传递给 RecorderViewController 可能不是两个控制器通信的最佳方式:您可能需要考虑使用 NSNotificationCenter,或传递模型对象并使用 键值观察

Are you setting the value of recorderViewController when you initialize the tableViewController (i.e., are you setting tableViewController.recorderViewController = self in your Recorder class)? If not, then your call to [recorderViewController changeView] is sending a message to nil – which doesn't crash, but it doesn't do anything either.

As an aside, passing your TableViewController a reference to your RecorderViewController is probably not the best way for two controllers to communicate: You may want to consider using NSNotificationCenter, or passing a model object and using Key-Value Observing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文