在iOS中的App Delegate中调用当前视图控制器中的方法
我有两个视图控制器(BuildingsViewController和RoomsViewController),都使用应用程序委托内的一个函数称为上传。 上传function基本上是一个HTTP请求,如果成功或失败,触发一个uialertview。 这工作正常。
我正在努力的部分来自应用程序委托的connectionDidFinishLoading方法。 我需要能够基本刷新当前视图控制器通过也许viewWillAppear方法的视图控制器。 在每个视图控制器的viewWillAppear函数中,我有确定底部工具栏上的button的代码。 
我想要通过应用程序代理完成上传时,每个视图控制器的工具栏中的“上传”button自动删除。
 我已经尝试从应用程序委托的connectionDidFinishLoading方法[viewController viewWillAppear:YES] ,但它永远不会被调用。 
我希望我很清楚。 任何帮助是极大的赞赏。
谢谢。
 如果已经显示视图,则不要调用viewWillAppear刷新视图。 你想要做的是以下几点: 
  ConnectionDidFinishLoading方法在通知后触发 
 [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshView" object:nil]; 
 在你的viewController观察这个通知。 您可以通过将此代码添加到init或viewDidLoad方法来完成此操作 
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshView:) name:@"refreshView" object:nil]; 
 现在在你的viewController实现-(void)refreshView:(NSNotification *) notification方法来pipe理你的视图。 
 如果您的目标是iOS 4.0及更高版本,则可以使用窗口的rootViewController属性来获取当前的视图控制器。 
 [window.rootViewController viewWillAppear]; 
 如果您希望应用程序在iOS 4.0之前的版本上运行,则可以将实例variables添加到应用程序委托中,以记住哪个视图控制器称为上upload方法,并将控制器自己作为参数发送。 
 - (void)upload:(UIViewController *)viewController { self.uploadingViewController = viewController; // This is the property you add ... } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self.uploadingViewController viewWillAppear]; self.uploadingViewController = nil; } 
 你也应该考虑使用不同的方法来重新加载button,像reloadButtons ,因为它与这种情况下出现的视图无关。 然后您将在viewWillAppear调用该方法。 
步骤1:
在您的App Delegate .h文件中,您需要声明如下所示的协议:
 @protocol AppConnectionDelegate <NSObject> @required -(void)connectionFinished:(NSObject*)outObject; @end 
在同一个文件中,像这样添加一个ivar:
 id *delegate; 
宣布伊娃为财产:
 @property (nonatomic, assign) id<AppConnectionDelegate> delegate; 
在App Delegate .m文件中,综合ivar:
 @synthesize delegate; 
在App Delegate .m文件中,在connectionDidFinishLoading上做:
 if([self.delegate respondsToSelector:@selector(connectionFinished:)]) { [self.delegate connectionFinished:objectYouWantToSend]; } 
在您的viewcontroller的.h文件中,通过导入对应用程序委托文件的引用来实现AppConnectionDelegate:
 #import "AppDelegate_iPhone.h" //if using iPhone #import "AppDelegate_iPad.h" //if using iPad 
在同一个文件中,在接口声明的第一行的末尾做:
 @interface AppDelegate_iPhone : AppDelegate_Shared <AppConnectionDelegate> 
相应地声明ivars:
 AppDelegate_iPhone *appDelegate; //if using iPhone AppDelegate_iPad *appDelegate; // if using iPad 
在viewDidLoad()中的viewcontroller的.m文件中,使用以下命令获取对应用程序委托的引用:
如果iPhone;
 appDelegate = (AppDelegate_iPhone*)[[UIApplication sharedApplication] delegate]; 
如果iPad:
 appDelegate = (AppDelegate_iPad*)[[UIApplication sharedApplication] delegate]; 
然后通过执行以下操作将viewcontroller设置为viewDidLoad()中的委托:
 appDelegate.delegate = self; 
现在你需要简单地在.m文件中实现connectionFinished方法:
 - (void)connectionFinished:(NSObject*)incomingObject { //Do whatever you want here when the connection is finished. IncomingObject is the object that the app delegate sent. } 
现在,只要您的应用程序委托的connectionDidFinishLoading被调用,视图控制器将被通知。
[这是一个最好的做法,如果你使用connectionFinishedcallback完成appDelegate.delegate = nil]
这是尝试和testing。 如果您有问题,请留下评论……
  – 编辑 –  
 这是NSNotification的一个强有力的select。 我使用两个取决于要求。 我用来决定使用NSNotification还是委托callback使用协议的过程很简单: 
 对于通知: 
 一个发件人,多个听众。 
 发送者和监听者之间没有可能的引用。 
 复杂/多个对象不需要发送 
 对于使用协议的委托callback: 
 一个发件人,有限的(通常是1个)听众。 
 发送者和监听者之间的引用是可能的。 
 复杂/多个对象将被发送(例如,需要发送的响应对象) 
 我知道通过通知发送对象是可能的,但我更喜欢这个协议。 
  – 编辑 –  
更糟糕的是,你可以让两个视图控制器遵循一个简单的方法协议,将删除该button并刷新视图。 然后在你的connectionDidFinishLoading方法中,因为你知道你的视图控制器必须遵守这个协议,通过你的devise,你只需要做一些像
 ViewController<MyProtocol> curView = (Get the current view controller somehow); [curview refreshView];