views:

37

answers:

3

hi everybody,

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?

A: 

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.

Vinzius
im not putting it at the same moment, when i move to second tab, in its viewdidload method, i use [self.view addSubview: currencyView]; but currview is deleted from first tabs viewconroller.
tester
Yeah, it's what I wanted to say. You can't have an UIView at two different place in the same time. (or you must do 2 different alloc).
Vinzius
hm, why? cant i use same uiviews copy in different views? i also tried to make instance for each tabs viewcontrollers (CurrnecyView alloc init etc..) then i set "self.currenyView=delegate.currView), but same thing. why cant i use the same uiview? i think i can make it copy, cant i?
tester
A: 

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

-(void)viewWillAppear {

AppDelegate_iPhone *delegate = [AppDelegate_iPhone sharedAppDelegate];
CurrencyView *cView = [delegate currView];
if(nil!=[cView superview) {
[cView removeFromSuperview];
}
[[self view]addSubview:cView];
}

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.

sigsegv
A: 

If what you want is a copy of the view, your @property statement for *cView should be

@Property (nonatomic, copy) UIView* cView;

if what you have instead is

@Property (nonatomic, retain) UIView* cView;

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.

fogelbaby