如何在选项卡中使用相同的UIView?
我有一个小的 UIView,我用来自网络的一些字符串数据填充它。 我只想加载一次。这就是为什么我在 appdelegate 类中调用它。
货币视图 *currView;
@property(非原子,保留)CurrencyView *currView;
在应用程序 didFinishLaunchingWithOptions:
CurrencyView *view=[[CurrencyView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
self.currView = view;
[view release];
然后我在每个视图控制器(在选项卡中)中调用它。
AppDelegate_iPhone *delegate = [AppDelegate_iPhone sharedAppDelegate];
self.currencyView= delegate.currView
[self.view addSubview: currencyView];
当我在选项卡中移动时,currView 从选项卡移动到其他选项卡。当返回到上一个选项卡时,货币视图消失了。它保留在最后一个选项卡上。我不知道为什么。
我应该怎么做才能将 curview 放入所有视图中?
i have a small UIView, i fill it with some string data from net.
i want to it load just once. thats why i call it in appdelegate class.
CurrencyView *currView;
@property (nonatomic, retain)CurrencyView *currView;
in application didFinishLaunchingWithOptions:
CurrencyView *view=[[CurrencyView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
self.currView = view;
[view release];
then i call it in every viewcontroller (in tabs).
AppDelegate_iPhone *delegate = [AppDelegate_iPhone sharedAppDelegate];
self.currencyView= delegate.currView
[self.view addSubview: currencyView];
when i move in tabs, currView moves from tab to other tab. when look back to the previous tab, currencyview is gone. it stays on the last tab. i dont know why.
what should i do for putting currview in all views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能同时将 UIView 放置到不同的控制器。当您将其添加到另一个控制器/视图时,它将从前一个控制器/视图中删除。
因此,当您更改选项卡时,您应该重新添加它^^(例如,通过使用委托方法。这取决于您使用的是什么;-))
我认为没有其他解决方案,但如果有人有更好的想法可以让我感兴趣。
You can't put an UIView to different controller at the same moment. When you add it to another controller / view, it will be removed from is previous one.
So you should re-add it when you change of tab ^^ (by using a delegate method, for example. It depends what you are using ;-))
I think there isn't another solution but if someone has a better idea it can interest me.
如果你想共享同一个视图实例,你可以在viewcontroller的viewWillAppear:方法中从appdelegate中检索该实例,检查视图的superview是否不为nil并最终将其从superview中删除,然后添加到当前视图。像这样的东西
也许你可以在 viewWillDisappear: 方法中从超级视图中删除 currView,而不是上面的方法。
尽管如此,这仍然很容易出错并且不是很干净。如果您只分配两个CurrencyView 实例并让它们共享公共数据/模型,那就更好了。
如果你的内存不足,我怀疑单个视图会杀死你的应用程序,但如果是这种情况,还有其他方法可以减少内存占用,请参阅Apple有关内存管理和视图控制器生命周期的文档。
If you want to share the same view instance, you can retrieve the instance from the appdelegate in the viewcontroller viewWillAppear: method, check if the view's superview is not nil and eventually remove it from the superview, then add it to the current view. Something like this
Maybe you can remove the currView from the superview in the viewWillDisappear: method rather than in the above.
Still, this remain very error prone and not very clean. It would be better if you just allocate two instance of CurrencyView and let them share the common data/model.
If you are low on memory, I doubt a single view will kill you app, but if that's the case there are other way to reduce the memory footprint, see the Apple's documentation on memory management and view controller lifecycle.
如果您想要的是视图的副本,则 *cView 的 @property 语句应该是,
如果您拥有的是
您试图在多个视图中共享同一个对象,但您不能这样做。对于前者,每次您从超级视图中获取它时,您都会制作它的副本,我认为这会起作用。
If what you want is a copy of the view, your @property statement for *cView should be
if what you have instead is
You are trying to share the same object in multiple views, which you can't do. With the former each time you grab it from the superview you will be making a copy of it, and I think that will work.