如何从 AppDelegate.m 调用 ViewController.m 方法

     2023-02-23     221

关键词:

【中文标题】如何从 AppDelegate.m 调用 ViewController.m 方法【英文标题】:How to call a ViewController.m method from the AppDelegate.m 【发布时间】:2014-01-25 17:09:35 【问题描述】:

在我的 ViewController.m 中,我有无效的“保存数据”:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize myDatePicker, myTextField;

-(void)saveData

NSArray *value = [[NSArray alloc] initWithObjects:[myTextField text],[myDatePicker date], nil];
[value writeToFile:[self getFilePath] atomically:YES];


我想在我的 AppDelegate.m 中使用我的无效“保存数据”:

- (void)applicationDidEnterBackground:(UIApplication *)application


[self.ViewController saveData];


但 Xcode 无法识别“ViewController”和“saveData”。

我不知道我是否必须在我的 AppDelegate 中#import 一些东西,请帮忙。

【问题讨论】:

当前答案是正确的 - 让您的视图控制器观察您要处理的通知。但是你的电话是非常规的。您是否有一个属性来引用您尝试调用其方法的视图控制器。编译器错误告诉您它们没有正确设置。 【参考方案1】:

您如何从应用程序委托访问您的视图控制器将取决于您拥有多少视图控制器/它在当前导航层次结构中的位置等。您可以为其添加一个属性,或者它可能是您的应用程序委托的@ 987654321@.

但是,当应用进入后台时,您最好在视图控制器中监听通知。这意味着您需要的逻辑可以完全包含在您的视图控制器中,而您的应用程序委托不需要知道任何有关它的信息。

在你的viewcontroller的初始化方法中,你可以注册接收通知:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(applicationDidEnterBackground:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];

然后,实现方法:

- (void)applicationDidEnterBackground:(NSNotification *)notification

    [self saveData];

并确保您在视图控制器的 dealloc 方法中也将自己移除为观察者:

-(void)dealloc 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

【讨论】:

我按照你说的做了,但是当我关闭应用程序时出现问题,一旦我想再次打开它,它就会崩溃并且无法打开。我不知道那里发生了什么。 ((在 NSNotification 后面应该有一个 * 对吧?)) 是的,应该有一个*,已经更新了我的答案。崩溃时您收到什么错误消息?它在哪一行中断? 没有错误信息。一旦我在运行 iOS 7 的 iOS 模拟器中安装了应用程序,关闭 xCode 并重新启动模拟器,它第一次工作,但是当我关闭并再次打开应用程序时,应用程序期望看到我“保存”的内容,应用程序只是没有不开。就这样..【参考方案2】:

在顶部的“appDelegate.m”实现文件中执行此导入 ->

#import "ViewController.h"

这将使自定义类 ViewController 对 appDelegate 可见。

为了能够从另一个类(在我们的例子中另一个类是 AppDelegate 类)调用 -(void)saveData 方法,这个方法需要在公共接口中声明。这意味着 ->

#import "ViewController.h"


@interface ViewController ()

-(void)saveData;

@end

【讨论】:

我按照你说的做了#import,但我仍然无法在我的 AppDelegate.m 中添加[self.viewController saveData],因为 Xcode 无法识别 viewController 也无法再次识别“saveData”。 self.ViewController 暗示您在 appDelegate 上创建了一个名为 ViewController 的属性。第一个问题是,它以大写字母开头。请将其重命名为 viewController。另一件事是,您实际上是否拥有该 viewController 的正确实例?也许这个属性没有初始化。你了解实例的概念吗?即使听起来很傻,我也需要问一下(对不起:)),否则我不确定您的理解有多深。 但是正确的“实例”对我来说听起来很新鲜。你可以帮我解决这个问题。 你贴的是ViewController类接口。我需要查看 AppDelegate.h 界面。

如何从 AppDelegate.m 访问 tabBarController?

】如何从AppDelegate.m访问tabBarController?【英文标题】:HowtoaccesstabBarControllerfromAppDelegate.m?【发布时间】:2014-11-2520:06:03【问题描述】:这是我的故事板:我正在尝试从AppDelegate.m中的方法访问tabBarController这是AppDelegate.h:#import<UI... 查看详情

AppDelegate.m 和 View Controller.m 的区别

】AppDelegate.m和ViewController.m的区别【英文标题】:DifferencebetweenAppDelegate.mandViewController.m【发布时间】:2011-05-1917:33:50【问题描述】:谁能告诉我在iPhone编程过程中何时使用AppDelegate.m和AppDelegate.h?我只使用ViewController.m和ViewControll... 查看详情

如何从 Xcode 中的 AppDelegate.m 文件更新 UILabel 的文本?

】如何从Xcode中的AppDelegate.m文件更新UILabel的文本?【英文标题】:howtoupdateaUILabel\'sTextfromAppDelegate.mfileinXcode?【发布时间】:2014-08-3014:51:50【问题描述】:我是Objective-C编程的新手。当应用程序进入后台时,我想从UILabel和至少NSLo... 查看详情

Cordova iOS:在 AppDelegate.m 中添加方法调用

】CordovaiOS:在AppDelegate.m中添加方法调用【英文标题】:CordovaiOS:AddmethodcallinAppDelegate.m【发布时间】:2016-06-2307:25:07【问题描述】:我正在为iOS构建一个带有cordova/ionic的应用程序由于多种原因,我们必须将代码放在AppDelegate.m的... 查看详情

如何将数据从 AppDelegate 传递到 ViewController?

】如何将数据从AppDelegate传递到ViewController?【英文标题】:HowtopassdatafromAppDelegatetoViewController?【发布时间】:2012-12-0305:18:54【问题描述】:我正在使用Safari浏览网页。单击此页面上的按钮后,我的Ipad将启动我的应用程序。所以... 查看详情

无法从 appdelegate.m 为 NSProgressIndicator 设置动画

】无法从appdelegate.m为NSProgressIndicator设置动画【英文标题】:UnabletoanimateNSProgressIndicatorfromappdelegate.m【发布时间】:2013-12-1121:03:29【问题描述】:我之前在IOS上工作过,但刚刚开始在MacOsX可可应用程序上工作。我遇到了关于NSProgr... 查看详情

从 AppDelegate.m 中的 applicationDidEnterBackground 中去掉键盘

】从AppDelegate.m中的applicationDidEnterBackground中去掉键盘【英文标题】:GetridofthekeyboardfromapplicationDidEnterBackgroundintheAppDelegate.m【发布时间】:2012-02-0714:53:47【问题描述】:我的MainViewController.m文件中有一个带有文本字段的应用程序。... 查看详情

来自 appDelegate 的 javascript 调用:phonegap iOS

】来自appDelegate的javascript调用:phonegapiOS【英文标题】:javascriptcallfromappDelegate:phonegapiOS【发布时间】:2011-10-2009:13:56【问题描述】:我如何在appDelegate.m类中获得phonegapwebview的实例我想在通知接收上调用javascript函数(即来自appDel... 查看详情

AppDelegate.swift 如何在 Xcode 6.3 中替换 AppDelegate.h 和 AppDelegate.m

】AppDelegate.swift如何在Xcode6.3中替换AppDelegate.h和AppDelegate.m【英文标题】:HowdoesAppDelegate.swiftreplaceAppDelegate.handAppDelegate.minXcode6.3【发布时间】:2015-05-2203:49:52【问题描述】:根据iOSDeveloperLibrary应用委托是您编写自定义应用级代码... 查看详情

如何从 View 对象调用 ContentView 函数?

】如何从View对象调用ContentView函数?【英文标题】:HowdoyoucallaContentViewfunctionfromaViewobject?【发布时间】:2020-02-2402:37:31【问题描述】:我有一个演示来说明我的问题,您可以在下面查看。我希望这个SwipeBar更改主ContentView的背景... 查看详情

如何在 AppDelegate.m 中获取现有的 UIViewController(不是 rootviewcontroller)?

】如何在AppDelegate.m中获取现有的UIViewController(不是rootviewcontroller)?【英文标题】:HowtogetanexistingUIViewController(notrootviewcontroller)inAppDelegate.m?【发布时间】:2014-08-2005:38:05【问题描述】:在我的AppDelegate.m中,我想获取情节提要... 查看详情

AppDelegate.m 的“application:didFinishLaunchingWithOptions”的默认实现是不是从 iOS 6 更改为 iOS 8?

】AppDelegate.m的“application:didFinishLaunchingWithOptions”的默认实现是不是从iOS6更改为iOS8?【英文标题】:DidAppDelegate.m\'sdefaultimplementationfor\'application:didFinishLaunchingWithOptions\'changefromiOS6toiOS8?AppDelegate.m的“applicatio 查看详情

如何从我的 ObservableObject 类调用我的 View 结构中的方法

】如何从我的ObservableObject类调用我的View结构中的方法【英文标题】:HowtocallamethodinmyViewstructfrommyObservableObjectclass【发布时间】:2020-10-1322:47:02【问题描述】:我正在为我的Picker选择使用@ObservedObjectPropertyWrapper,并且我想在选择... 查看详情

如何在 AppDelegate 中执行 Segue?

】如何在AppDelegate中执行Segue?【英文标题】:HowtoperformSegueinAppDelegate?【发布时间】:2012-08-3114:25:51【问题描述】:我正在尝试使用Storyboard在IOS5.1上完成应用程序。基本上我正在做一个保管箱应用程序。因为我使用的是DropboxSDK... 查看详情

在 AppDelegate.m 上设置 label.text

】在AppDelegate.m上设置label.text【英文标题】:Setlabel.textoverAppDelegate.m【发布时间】:2012-05-0710:28:03【问题描述】:我有ViewController,由于某些原因,我必须从AppDelegate.m设置label.text我该怎么做?我已尝试使用此代码,但没有成功:... 查看详情

从 appdelegate 设置 UIViewController 委托

】从appdelegate设置UIViewController委托【英文标题】:setUIViewControllerdelegatefromappdelegate【发布时间】:2014-03-0617:25:02【问题描述】:我正在尝试从我的应用委托中设置视图控制器的委托。但它不起作用。AppDelegate.m:UIStoryboard*sb=[UIStoryb... 查看详情

源码03-02-09-控制器view懒加载

  ////AppDelegate.m//09-控制器View懒加载#import"AppDelegate.h"#import"ViewController.h"@interfaceAppDelegate()@end@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishL 查看详情

未找到“RCTBundleURLProvider.h”文件 - AppDelegate.m

】未找到“RCTBundleURLProvider.h”文件-AppDelegate.m【英文标题】:"RCTBundleURLProvider.h"filenotfound-AppDelegate.m【发布时间】:2017-03-1501:44:44【问题描述】:我正在尝试在XCode中运行我的ReactNative应用程序,但我不断收到此错误。我... 查看详情