I want to make a program that parses an XML file and then updates labels on 2 different tab bar views. On both of these views, I have a refresh button to update the results. What I'd like to do is update both of the views labels from either view. I figure the AppDelegate is probably a good choice to make this happen, but I tried creating classes that the AppDelegate can access but since they're instances of a class they contain no values. I get no errors but the labels don't update even though the data changes. This is the method in my AppDelegate that is called after the XML is parsed:
我想制作一个程序来解析XML文件,然后更新2个不同标签栏视图上的标签。在这两个视图中,我都有一个刷新按钮来更新结果。我想要做的是从任一视图更新两个视图标签。我认为AppDelegate可能是一个很好的选择,但我尝试创建AppDelegate可以访问的类,但由于它们是类的实例,因此它们不包含任何值。我没有错误,但即使数据发生变化,标签也不会更新。这是我的AppDelegate中解析XML后调用的方法:
-(void)callUpdateValues {
NSLog(@"Calling Update from AppDelegate");
home *homeController;
data *dataController;
[homeController updateValues];
[dataController updateValues];
}
One of the update methods looks like:
其中一种更新方法如下:
- (void)updateValues {
NSLog(@"Call Home");
[label1 setText: [[[[totalData objectAtIndex:0] objectForKey:@"nodeChildArray"] objectAtIndex:7] valueForKey:@"nodeContent"]];
[label2 setText:[[[[totalData objectAtIndex:0] objectForKey:@"nodeChildArray"] objectAtIndex:1] valueForKey:@"nodeContent"]];
}
So the view calls the AppDelegate method "callUpdateValues" and then that method should call the individual "updateValues" methods in each view. I am not an expert on this by any means, and I'm really just trying to get an idea of how programming on the iPhone works. I'm probably just not understanding something here, and if someone could give me some sort of answer I'd appreciate it.
因此视图调用AppDelegate方法“callUpdateValues”,然后该方法应在每个视图中调用单独的“updateValues”方法。我无论如何都不是这方面的专家,我真的只是想知道iPhone上的编程是如何工作的。我可能只是在这里不了解一些东西,如果有人能给我某种答案,我会很感激。
3 个解决方案
#1
Cocoa has a number of classes available for notifying interested parties of changes. Directly calling methods as you describe makes things much more closely coupled than you need to.
Cocoa有许多课程可用于通知感兴趣的各方变更。直接调用您描述的方法可以使事物与您需要的联系更紧密。
In your method that generates the update you'd have:
在生成更新的方法中,您将拥有:
[[NSNotificationCenter defaultCenter] postNotificationName:@"IGotSomeNewData"
object:newData
userInfo:nil];
And in the classes that want to hear about updates you'd register for the notification:
在想要了解更新的课程中,您需要注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(newStuff:)
name:@"IGotSomeNewData" object:nil];
And then implement the method that gets called when something happens:
然后实现在发生事件时调用的方法:
- (void) newStuff: (NSNotification *)notification {
id newData = [notification object];
// Do stuff
}
#2
There's some really great stuff getting done by Apple for XML on the iPhone: XML Reading Material
Apple在iPhone上为XML做了一些非常棒的事情:XML阅读材料
The first snippet is out of place. I think what you're missing is that you need to create your instances within the AppDelegate.h, expose them using properties (and synthesizing them in the .m). Then you're update structure should fit better.
第一个片段不合适。我认为您缺少的是您需要在AppDelegate.h中创建实例,使用属性公开它们(并在.m中合成它们)。然后你的更新结构应该更合适。
If you're just picking up iPhone programming, start digging into the guides that apple provides, and even if you're not into that, start pulling down at least 5 sample code projects a day. The beauty of them is that you can build them (even onto your iphone) and if you like a feature, you can see how it's done. Alternatively, get the grapefruit book from APRESS. Beginning iPhone. Hope this helped.
如果您只是选择iPhone编程,请开始深入了解苹果提供的指南,即使您没有参考,也要开始每天至少下拉5个示例代码项目。它们的美妙之处在于你可以构建它们(甚至是你的iphone),如果你喜欢它的功能,你可以看到它是如何完成的。或者,从APRESS获取葡萄柚书。开始iPhone。希望这有帮助。
#3
In the example you gave, homeController
and dataController
are not properly initialized. If I understand your project correctly, you would have created instances of the homeController
and dataController
classes in your main XIB file, and connected them up to the appropriate views (label1
and label2
). Your AppDelegate should, then, look something like this:
在您给出的示例中,homeController和dataController未正确初始化。如果我正确理解了您的项目,您将在主XIB文件中创建homeController和dataController类的实例,并将它们连接到相应的视图(label1和label2)。那么你的AppDelegate应该是这样的:
...
@class homeController;
@class dataController;
@interface AppDelegate
{
IBOutlet homeController * home;
IBOutlet dataController * data;
}
...
@end
With this in place, you would add (in your application XIB file), links from your homeController
and dataController
instances to the appropriate outlets (labeled home
and data
) in your application delegate.
有了这个,您可以添加(在您的应用程序XIB文件中)从homeController和dataController实例到应用程序委托中相应插座(标记为home和data)的链接。
Then, you could simply reference them by name in your callUpdateValues
method:
然后,您可以在callUpdateValues方法中按名称引用它们:
-(void)callUpdateValues {
NSLog(@"Calling Update from AppDelegate");
[home updateValues];
[data updateValues];
}
On a side note, Cocoa coding standards usually specify that class names are capitalized. This is, of course, up to your personal taste, but if you're just getting started in Cocoa, it may be worth drinking one more cup of kool-aid at this point, just so your code will "fit in" with what most other developers are doing. Again, totally up to you!
另外,Cocoa编码标准通常指定类名称是大写的。当然,这取决于您的个人品味,但如果您刚刚开始使用Cocoa,那么此时可能还需要再喝一杯kool-aid,这样您的代码就会“适应”什么大多数其他开发人员正在做再一次,完全取决于你!
#1
Cocoa has a number of classes available for notifying interested parties of changes. Directly calling methods as you describe makes things much more closely coupled than you need to.
Cocoa有许多课程可用于通知感兴趣的各方变更。直接调用您描述的方法可以使事物与您需要的联系更紧密。
In your method that generates the update you'd have:
在生成更新的方法中,您将拥有:
[[NSNotificationCenter defaultCenter] postNotificationName:@"IGotSomeNewData"
object:newData
userInfo:nil];
And in the classes that want to hear about updates you'd register for the notification:
在想要了解更新的课程中,您需要注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(newStuff:)
name:@"IGotSomeNewData" object:nil];
And then implement the method that gets called when something happens:
然后实现在发生事件时调用的方法:
- (void) newStuff: (NSNotification *)notification {
id newData = [notification object];
// Do stuff
}
#2
There's some really great stuff getting done by Apple for XML on the iPhone: XML Reading Material
Apple在iPhone上为XML做了一些非常棒的事情:XML阅读材料
The first snippet is out of place. I think what you're missing is that you need to create your instances within the AppDelegate.h, expose them using properties (and synthesizing them in the .m). Then you're update structure should fit better.
第一个片段不合适。我认为您缺少的是您需要在AppDelegate.h中创建实例,使用属性公开它们(并在.m中合成它们)。然后你的更新结构应该更合适。
If you're just picking up iPhone programming, start digging into the guides that apple provides, and even if you're not into that, start pulling down at least 5 sample code projects a day. The beauty of them is that you can build them (even onto your iphone) and if you like a feature, you can see how it's done. Alternatively, get the grapefruit book from APRESS. Beginning iPhone. Hope this helped.
如果您只是选择iPhone编程,请开始深入了解苹果提供的指南,即使您没有参考,也要开始每天至少下拉5个示例代码项目。它们的美妙之处在于你可以构建它们(甚至是你的iphone),如果你喜欢它的功能,你可以看到它是如何完成的。或者,从APRESS获取葡萄柚书。开始iPhone。希望这有帮助。
#3
In the example you gave, homeController
and dataController
are not properly initialized. If I understand your project correctly, you would have created instances of the homeController
and dataController
classes in your main XIB file, and connected them up to the appropriate views (label1
and label2
). Your AppDelegate should, then, look something like this:
在您给出的示例中,homeController和dataController未正确初始化。如果我正确理解了您的项目,您将在主XIB文件中创建homeController和dataController类的实例,并将它们连接到相应的视图(label1和label2)。那么你的AppDelegate应该是这样的:
...
@class homeController;
@class dataController;
@interface AppDelegate
{
IBOutlet homeController * home;
IBOutlet dataController * data;
}
...
@end
With this in place, you would add (in your application XIB file), links from your homeController
and dataController
instances to the appropriate outlets (labeled home
and data
) in your application delegate.
有了这个,您可以添加(在您的应用程序XIB文件中)从homeController和dataController实例到应用程序委托中相应插座(标记为home和data)的链接。
Then, you could simply reference them by name in your callUpdateValues
method:
然后,您可以在callUpdateValues方法中按名称引用它们:
-(void)callUpdateValues {
NSLog(@"Calling Update from AppDelegate");
[home updateValues];
[data updateValues];
}
On a side note, Cocoa coding standards usually specify that class names are capitalized. This is, of course, up to your personal taste, but if you're just getting started in Cocoa, it may be worth drinking one more cup of kool-aid at this point, just so your code will "fit in" with what most other developers are doing. Again, totally up to you!
另外,Cocoa编码标准通常指定类名称是大写的。当然,这取决于您的个人品味,但如果您刚刚开始使用Cocoa,那么此时可能还需要再喝一杯kool-aid,这样您的代码就会“适应”什么大多数其他开发人员正在做再一次,完全取决于你!