Iphone iOS从代理访问视图数据而不分配新视图
我需要在不分配新视图的情况下更改应用程序委托方法中的数据(标签)。该视图称为“提醒”,因此我将其导入到代理中,并且只有在分配该视图时才能访问其数据(提醒*anything=[提醒alloc…等),但由于我想更改当前加载的视图,因此我需要直接访问已加载的视图 当我的应用程序进入前台时,如何从代理更改主视图的标签 obs:我知道我可以在Iphone iOS从代理访问视图数据而不分配新视图,iphone,ios,xcode,delegates,uilocalnotification,Iphone,Ios,Xcode,Delegates,Uilocalnotification,我需要在不分配新视图的情况下更改应用程序委托方法中的数据(标签)。该视图称为“提醒”,因此我将其导入到代理中,并且只有在分配该视图时才能访问其数据(提醒*anything=[提醒alloc…等),但由于我想更改当前加载的视图,因此我需要直接访问已加载的视图 当我的应用程序进入前台时,如何从代理更改主视图的标签 obs:我知道我可以在-(void)viewdiload或-(void)view上完成,但它不会解决我的问题,因为它不会改变标签,例如,如果用户通过通知框(手机锁定时的幻灯片图标)打开应用
-(void)viewdiload
或-(void)view上完成,但它不会解决我的问题,因为它不会改变标签,例如,如果用户通过通知框(手机锁定时的幻灯片图标)打开应用程序。在这种情况下,如果应用程序在后台打开,则不会调用上述任何方法
我不知道我是否清楚,希望我清楚。提前谢谢你。也许你可以尝试以下代码-
NSMutableArray *activeControllerArray = [self.navigationController.viewControllers mutableCopy];
for (int i = 0; i< [activeControllerArray count]; i++) {
if ([[activeControllerArray objectAtIndex:i] isKindOfClass:[Reminder Class]) {
Reminder *object = [activeControllerArray objectAtIndex:i];
//Perform all the task here which you want.
break; //Once found break the loop to do further processing.
}
}
NSMutableArray*activeControllerArray=[self.navigationController.viewControllers mutableCopy];
对于(int i=0;i<[activeControllerArray计数];i++){
如果([[activeControllerArray objectAtIndex:i]是类的种类:[提醒类]){
提醒*对象=[activeControllerArray对象索引:i];
//在这里执行您想要的所有任务。
break;//一旦找到,请中断循环以进行进一步处理。
}
}
只需从applicationIdentiterForeGround:方法发送一个通知,并在您想要更新标签的类上接收它,如下所示
//Your ApplicationDidEnterForeground:
[[NSNotificationCenter defaultCenter] postNotificationWithName:@"UpdateLabel" withObject:nill];
并在要更新标签的控制器的viewDidLoad:中添加观察者
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateLabel:)
name:@"UpdateLabel"
object:nil];
让你的方法在同一个班级
- (void)updateLabel:(NSNotification *)notification{
update label
}
如果您使用的是故事板,则可以执行此操作以访问正在查看的当前视图
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UINavigationController *a=_window.rootViewController;
Reminder *rem = a.topViewController;
rem.label.text=@"test";
}
如果不使用故事板
当我创建以后需要访问的视图时,我将它们定义为属性,如下所示
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//codes before here
self.reminder = [[Reminder alloc] init];
self.reminder.label.text = @"OLD LABEL";
//codes after here
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
self.reminder.label.text = @"NEW LABEL";
}
在AppDelegate.h上
//@interface SIMCAppDelegate : UIResponder <..........>
//{
//Some variables here
//}
//Properties here
@property (strong, nonatomic) Reminder *reminder;
//Some method declaration here
//eg: -(void) showSomething;
所以当我像这样分配/初始化视图时
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//codes before here
self.reminder = [[Reminder alloc] init];
self.reminder.label.text = @"OLD LABEL";
//codes after here
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
self.reminder.label.text = @"NEW LABEL";
}
在其他方法上分配后,我将能够再次访问它,如下所示
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//codes before here
self.reminder = [[Reminder alloc] init];
self.reminder.label.text = @"OLD LABEL";
//codes after here
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
self.reminder.label.text = @"NEW LABEL";
}
谢谢您的回答@rishi。只有一个问题,Xcode无法识别“self.navigationController”。还有什么我应该做的吗?你的应用程序委托包含导航控制器的属性?我现在创建了一个。Xcode中没有问题,但当我在你的代码中添加断点时,应用程序正在跳过if语句。你可以尝试记录并检查是否存在任何视图控制器,可能存在issue具有导航层次结构。仅创建导航控制器是不够的。要使此代码正常工作,导航控制器必须是您窗口的根控制器,并且必须通过它添加所有其他控制器。感谢您的回答。我已经用类似的方法进行了测试,但为了以防万一,我再次进行了测试,并遵循了所有步骤和你写的一模一样。但是,标签没有改变。还有其他猜测吗?事实上,标签甚至没有显示“旧标签”。它只是保持空白。是的,我已经连接了故事板中的所有内容,我可以从提醒内更改标签。m…我只是不能在提醒外更改它。当你在applicationWillEnterForeground设置断点时,它会在那里中断吗?顺便问一下,applicationWillEnterForeground!=你的q中说明的ApplicationIdentiterForegroundUESTIONU如何从insident.m更改标签?您调用了什么代码/方法来更改它,因此我将有更多的想法用于测试目的,我刚刚从members.m viewDidLoad method.label.text=@“任何东西”更改了标签。我已经删除了它,以防止与委托方法发生冲突。有没有一种方法可以实时执行操作?例如,不依赖于委托标准方法?例如,如果通知到达,请同时更改标签,而不完全依赖于应用程序状态?这很好!我不知道为什么没有人对此投反对票s