views:

236

answers:

2

Hi all,

The app is a viewbased project type. I am using a NSObject in a View XIB(UIView Nib), assume this object to be the topbar of the app.

MainController UIViewController loads the initial screen. it looks like this,

UIViewController MainViewController Image

i wanted to load a Top view(new one, call it X) Designed separately in the topbarview(red colour) in the above image.

Now,I have NSObject which resides inside "X" and binds all the events and have full control over X, its a singleton object.

in the NSObject Class i have a method to return the self object instance and a instance of uiview("X") it controls.

The methods are ,

-(UIView*)ReturnTopBarView
{
            NSLog(@"called 2");
    return topBarObject.topBarView;
}

//Provides the top bar instance to all the view controllers.
//Singleton Implementation.
+ (id)GetTopBarObject
{
    if(topBarObject == nil)
    {
        NSLog(@"called 1");
        return [[TopBarObject alloc] init];
    }
    return topBarObject;    
}

The topbarview is the fully designed topbarview("X") which has to come in the main view controller's topbarview(red color segment in the image above).

i wrote a method in the main view controller as :

-(void)SetHomeScreen
{
    self.topView = [[TopBarObject GetTopBarObject] ReturnTopBarView];
}

i invoke the -(void)SetHomeScreen in my appdelegate.applicationdidfinishlaunching as [viewcontroller SetHomeScreen]

This actually calls the corresponding methods in the NSObject(TopBarObject class), but i dont see the view updated in (mainviewcontroller.topbarview). This is the main issue now.

i have a custom top bar and custom bottom bar with images and icons. So i chose this way of designing. I am not sure whether this is the best way, I am just seeking for a most optimal system design to make this work efficient. If you think there is some other way which is far far good than this please share it with me.

A: 

I see that the singleton is incorrect. It should be something like this:

+ (id)GetTopBarObject {
    if(topBarObject == nil) {
        topBarObject = [[TopBarObject alloc] init];
    }
    return topBarObject;
}

In your code the topBarObject is never initiated and therefore each time GetTopBarObject is called it generates a new TopBarObject and returns it...

Michael Kessler
ya, you are right. I noticed just now. Thanks a lot. But this doesnt solve the issue right? please correct me if i am wrong.
Futur
I think it should solve the problem. You write "return topBarObject.topBarView;" in "ReturnTopBarView" method but the topBarObject is not initiated at all. BTW, why do you return "topBarObject.topBarView" and not the "topBarView" directly? - this is not a static method...
Michael Kessler
ya thats a question puts some thoughts in my mind.. but do you think this is a right implementation on the whole.. im getting this question raised now after your question? could you please help me suggestion the proper structure..
Futur
If the top part should stay the same in all the screens then there are 2 good practices:(1) as you do, (2) create one main view that will always be active and make the navigation controller's view be a subview of this main view. In the second option the common parts will stay static when switching between screens...
Michael Kessler
A: 

Where in appdelegate.applicationdidfinishlaunching is it called ?
If you are calling before the view is actually setup all you do is setting nil with something.
Try a viewcontroller.view; before [viewcontroller SetHomeScreen]

CiNN
oh ok.. let me try that now then..
Futur
yeah the controller has the view now... but am i following the right pattern? is this way of loading a app which has a custom top and bottom bar in a app? please guide me here
Futur