iOS页面的转向

在做页面转向的时候,调用:

[self.navigationController pushViewController:testSecond animated:YES];

发现始终不正确,后面的NSLog都输出出来了。


调试了一下,发现这个

self.navigationController 

是nil的,所以怎么进行push呢


经过修改,需要在AppDelegate中加入对navigationController的初始化:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self.window makeKeyAndVisible];
    
    TestViewController *testController = [[TestViewController alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:testController];
    self.window.rootViewController = nav1;
    [testController release];
    [nav1 release];
    
    return YES;
}

总得测试代码如下:

AppDelegate.m:

//
//  AppDelegate.m
//  Demo
//
//  Created by bjsmit01 on 13-10-16.
//  Copyright (c) 2013年 JXL. All rights reserved.
//

#import "AppDelegate.h"
#import "TestViewController.h"
#import "StartVC.h"

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self.window makeKeyAndVisible];
    
    TestViewController *testController = [[TestViewController alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:testController];
    self.window.rootViewController = nav1;
    [testController release];
    [nav1 release];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

TestViewController.h:

//
//  TestViewController.h
//  Demo
//
//  Created by zengraoli on 13-10-19.
//  Copyright (c) 2013年 JXL. All rights reserved.
//

#import <UIKit/UIKit.h>

@class TestSecondViewController;

@interface TestViewController : UIViewController

- (BOOL) connectedToNetwork;

@property(nonatomic,retain)TestSecondViewController *testSecond;

@end

TestViewController.m:

//
//  TestViewController.m
//  Demo
//
//  Created by zengraoli on 13-10-19.
//  Copyright (c) 2013年 JXL. All rights reserved.
//

#import "TestViewController.h"
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonHMAC.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netdb.h>
#import <arpa/inet.h>
#import "TestSecondViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

@synthesize testSecond;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title=@"登陆界面";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(20, 20, 280, 40);
    btn.backgroundColor = [UIColor clearColor];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal];
    
    [btn addTarget:self
                action:@selector(testOut:)
      forControlEvents:UIControlEventTouchUpInside
     ];
    
    [self.view addSubview:btn];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)testOut:(UIButton *)sender
{
    if(![self connectedToNetwork])
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"网络连接失败,请查看网络是否连接正常!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

    else{
//        testSecond = [[TestSecondViewController alloc] initWithNibName:@"TestSecondViewController" bundle:nil];
        testSecond = [[TestSecondViewController alloc] init];
        
        if (self.navigationController == nil) {
            NSLog(@"self.navigationController is nil");
        }
        
        [self.navigationController pushViewController:testSecond animated:YES];
    }
    

    NSLog(@"testOut Event");
}

-(BOOL) connectedToNetwork
{
    // Create zero addy
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    
    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
    
    if (!didRetrieveFlags)
    {
        printf("Error. Could not recover network reachability flags\n");
        return NO;
    }
    
    BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
    BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
    return (isReachable && !needsConnection) ? YES : NO;
}

@end

TestSecondViewController.h:

//
//  TestSecondViewController.h
//  Demo
//
//  Created by zengraoli on 13-10-19.
//  Copyright (c) 2013年 JXL. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TestSecondViewController : UIViewController

@end

TestSecondViewController.m:

//
//  TestSecondViewController.m
//  Demo
//
//  Created by zengraoli on 13-10-19.
//  Copyright (c) 2013年 JXL. All rights reserved.
//

#import "TestSecondViewController.h"

@interface TestSecondViewController ()

@end

@implementation TestSecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    NSLog(@"viewDidLoad");
    
//    UILabel *lab = [[UILabel alloc] init];
//    lab.frame = CGRectMake(100, 100, 100, 61);
//    lab.text = @"LABEL Test";
//    
//    [self.view addSubview:lab];
//    [lab release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


效果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值