开发者

Cocoa - Need help in switching xibs

I would really appreciate any help on where I am going wrong. Essentially I have a game app I am developing in XCode4 (Universal) - I have several subview screens (.h .m .xib) that I need to switch between and in some cases reload. Here is how I am doing it in the main AppDelegate.m:

-(void)switchxibs_nextScreen1 {

//close any views first
[self closesuperviews];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{    
    nextGame *theView = [[nextGame alloc] init]; 
    theView.view.tag=101;
    [_window addSubview:theView.view];  
}
else
{  
    nextGame_iPhone *theView = [[nextGame_iPhone alloc] init]; 
    theView.view.tag=101;
    [_window addSubview:theView.view]; 
} 
}

-(void) switchxibs_nextScreen2 {
//close any views first
[self closesuperviews];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{    
    nextGame *theView = [[nextGame2 alloc] init]; 
    theView.view.tag=101;
    [_window addSubview:theView.view];  
}
else
{  
    nextGame_iPhone *theView = [[nextGame2_iPhone alloc] init]; 
    theView.view.tag=101;
    [_window addSubview:theView.view]; 
} 
}
-(void)closesuperviews{ 
for (UIView *subview in _window.subviews) { 
    // Only remove the subviews with tag equal to 101
    if(subview.tag==101){
        [subview removeFromSuperview];  
    }

} 
}

I am not sure if thi开发者_运维百科s is the way I am supposed to do it. I am running into issues where when switching screens and going back to one it appears that it wasnt properly closed. Any help would be greatly appreciated.


First, you should know that you can eliminate some redundancy in your code by simply naming your xibs with correct format. iOS has certain naming conventions for resources (XIBs, images, et) which allow the OS to load the appropriate resource for different devices. From the iOS Resource Programming Guide...

iOS Supports Device-Specific Resources

In iOS 4.0 and later, it is possible to mark individual resource files as usable only on a specific type of device. This capability simplifies the code you have to write for Universal applications. Rather than creating separate code paths to load one version of a resource file for iPhone and a different version of the file for iPad, you can let the bundle-loading routines choose the correct file. All you have to do is name your resource files appropriately.

To associate a resource file with a particular device, you add a custom modifier string to its filename. The inclusion of this modifier string yields filenames with the following format:

<basename><device>.<filename_extension>

...The <device> string is a case-sensitive string that can be one of the following values:

~ipad - The resource should be loaded on iPad devices only.

~iphone - The resource should be loaded on iPhone or iPod touch devices only.

So you don't need to do all the manual checking your doing. As for the actual act of switching, knowing more about your app/view structure would help in answering the question, but you should probably be using instances of UIViewController to manage your views, not managing them directly in the app delegate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜