UINavigation的订制

创建一个windows-based的application,一个loginviewcontroller,一个joinviewcontroller,  基于导航的,都是代码实现界面,没有用xib。

出现一个bug,就是一直切换不成功,原来有个很小很小的问题:

Delegate的.h文件里添加

LoginViewController *loginViewController;
UINavigationController *navigationController;

 

@implementation CoLinkersAppDelegate

代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    
     
    loginViewController 
= [[LoginViewController alloc] init];
    navigationController 
= [[UINavigationController alloc]
                                                  initWithRootViewController:loginViewController];
    navigationController.navigationBar.tintColor
=[UIColor colorWithRed:(247.0/255.0) green:(126.0/255) blue:(33.0/255) alpha:1.0];
    
    
    loginViewController.title 
= @"CoLinkers";
    
// Override point for customization after application launch
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
    
    
return YES;
}

在loginviewcontroller.m里

代码
- (id)init
{
    
if (self = [super init])
    {
        self.view 
= [[[UIView alloc] initWithFrame:
                      [[UIScreen mainScreen] applicationFrame]] autorelease];
        
        self.view.backgroundColor 
= [UIColor colorWithRed:(255.0/255.0) green:(230.0/255) blue:(176.0/255) alpha:1.0];
        
        UIBarButtonItem 
*joinButton = [[UIBarButtonItem alloc] 
                                       initWithImage:[UIImage imageNamed:
@""
                                       style:UIBarButtonItemStylePlain
                                       target:self 
                                       action:@selector(join)];
        joinButton.title
=@"Join";
        self.navigationItem.leftBarButtonItem 
= joinButton;
        
        
// add a UILabel
        /*label = [[UILabel
                  alloc] initWithFrame:CGRectMake(70, 20, 180, 60)];
        label.text = @"CoLinkers";
        label.backgroundColor = [UIColor clearColor];
        label.shadowColor = [UIColor grayColor];
        label.textAlignment = UITextAlignmentCenter;
        label.font = [UIFont fontWithName:@"Helvetica" size:36.0];
        label.textColor = [UIColor whiteColor];
        [self.view addSubview:label];
        [label release];
*/
        
        
//add Table
        UITableView *myBeaconsTableView = [[UITableView alloc] 
                                           initWithFrame:CGRectMake(
028, self.view.bounds.size.width, 100
                                           style:UITableViewStyleGrouped];
        
        myBeaconsTableView.backgroundColor 
= [UIColor clearColor];
        myBeaconsTableView.
delegate=self;
        myBeaconsTableView.dataSource
=self;
        [self.view addSubview:myBeaconsTableView];
        [myBeaconsTableView release];

        
        session 
= [FBSession sessionForApplication:@"2aaef51db6d3cb75c0873804434fce88" secret:@"83492d958b359575113be63dc16ad527" delegate:self];
        button 
= [[[FBLoginButton alloc] init] autorelease]; 
        [button setCenter:CGPointMake(
161.0f160.0f)];
        [self.view addSubview:button];
    }
    
return self;
}

就可以导入loginviewcontroller界面,我里面附加了facebook登陆代码

在loginviewcontroller里有个join按钮

代码
- (IBAction)join{
    
    joinViewController 
= [[JoinViewController alloc] init];
    
//joinNavController = [[[UINavigationController alloc]
                            
// initWithRootViewController:joinViewController] autorelease];
       
// joinNavController.navigationBar.tintColor=[UIColor colorWithRed:(247.0/255.0) green:(126.0/255) blue:(33.0/255) alpha:1.0];
    
//[self presentModalViewController:joinNavController animated:YES];

    [self.navigationController pushViewController:joinViewController animated:YES];
}

在joinviewcontroller.m

代码
- (id)init
{
        NSLog(
@"2");
    
if (self = [super init])
    {
        NSLog(
@"12");
         self.title 
= @"Join"
        self.view 
= [[[UIView alloc] initWithFrame:
                      [[UIScreen mainScreen] applicationFrame]] autorelease];
        
        self.view.backgroundColor 
= [UIColor colorWithRed:(254.0/255.0) green:(250.0/255) blue:(239.0/255) alpha:1.0];
        
        UIBarButtonItem 
*joinButton = [[UIBarButtonItem alloc] 
                                       initWithImage:[UIImage imageNamed:
@""
                                       style:UIBarButtonItemStylePlain
                                       target:self 
                                       action:@selector(joinAction)];
        joinButton.title
=@"Join";
        self.navigationItem.rightBarButtonItem 
= joinButton;
        
        
        
        
//add Table
        UITableView *myBeaconsTableView = [[UITableView alloc] 
                                           initWithFrame:CGRectMake(
028, self.view.bounds.size.width, 300
                                           style:UITableViewStyleGrouped];
        
        myBeaconsTableView.backgroundColor 
= [UIColor clearColor];
        myBeaconsTableView.
delegate=self;
        myBeaconsTableView.dataSource
=self;
        [self.view addSubview:myBeaconsTableView];
        [myBeaconsTableView release];
        
    }
    
return self;
}

就实现了两个界面的切换。

 

可是我代码是对的,但是一直切换不成功

 找了半天 发现是aotureleace惹的祸

因为之前在 CoLinkersAppDelegate里创建navigation的时候,我是这样的

navigationController = [[[UINavigationController alloc]
                                                  initWithRootViewController:loginViewController] aoturelese];

以至于切换不了。后来改为

navigationController = [[UINavigationController alloc]
                                                  initWithRootViewController:loginViewController]; 

然后在dealloc里去

[navigationController release];

切记切记,不能随意 aoturelese。

 

关于aoturelease的详细介绍可参见http://www.cnblogs.com/VinceYuan/archive/2010/03/08/1680488.html

上面的原因应该不是aoturelease吧,还有待继续研究?

 

 

 

 

posted on 2010-03-12 11:30  fairycao  阅读(542)  评论(0)    收藏  举报