2012-06-26 31 views
1

我發現很難開始使用Objective-C。iPhone Objective C - 無法顯示新視圖

我執行下面的代碼在單擊按鈕時:

NSLog(@"hi"); 
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil]; 
[[self navigationController]pushViewController:mainMenuDriver animated:YES]; 

我可以看到「喜」在控制檯當我按下按鈕,它只是認爲應該改爲MainMenuDriver。但沒有任何反應!

請幫忙!

根據要求更多的代碼:

MainMenuDriver.h:

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 

@interface MainMenuDriver : UIViewController <UINavigationControllerDelegate,CLLocationManagerDelegate>{ 
    IBOutlet UIButton *photos; 
    IBOutlet UIButton *profile; 

    IBOutlet UISwitch *onOffline; 

    IBOutlet UILabel *label1; 

    NSTimer *uploadGPS_timer; 

    CLLocationManager *lm; 
    NSString *lat; 
    NSString *lng; 
} 
@property(nonatomic,retain) UIButton *photos; 
@property(nonatomic,retain) UIButton *profile; 
@property(nonatomic,retain) UISwitch *onOffline; 
@property(nonatomic,retain) UILabel *label1; 
@property (nonatomic,retain) NSTimer *uploadGPS_timer; 
@property(nonatomic,retain) NSString *lat,*lng; 

-(IBAction)showMessages:(id)sender; 
-(IBAction)showFriends:(id)sender; 
-(IBAction)showPhotos:(id)sender; 
-(IBAction)showProfile:(id)sender; 

-(IBAction)switchSwitched:(id)sender; 

-(void)uploadGPS_tick:(NSTimer*)timer; 
@end 

MainMenuDriver.m

#import "MainMenuDriver.h" 
#import "ASIFormDataRequest.h" 
#import "JoeMaxiViewController.h" 
#import "Photos.h" 
#import "Profile.h" 

@implementation MainMenuDriver 
@synthesize messages,profile,photos,friends,label1; 
@synthesize onOffline; 
@synthesize uploadGPS_timer; 
@synthesize lat,lng; 

-(IBAction)showPhotos:(id)sender{ 
    [self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]]; 
    Photos *x= [[Photos alloc] initWithNibName:nil bundle:nil]; 
    [[self navigationController]pushViewController:x animated:YES]; 
} 
-(IBAction)showProfile:(id)sender{ 
    [self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]]; 
    Profile *x= [[Profile alloc] initWithNibName:nil bundle:nil]; 
    [[self navigationController]pushViewController:x animated:YES]; 
} 
-(void)logout:(id)sender{ 
    if([uploadGPS_timer isValid]){ 
     [uploadGPS_timer invalidate]; 
    } 
    [lm release]; 
    //[uploadGPS_timer release]; 

    [self.navigationController popViewControllerAnimated:NO]; 




    /*NSString *urlStr=[[NSString alloc] initWithFormat:@"http://www.prestocab.com/driver/ajax/logout.php"]; 
    NSURL *url=[NSURL URLWithString:urlStr]; 
    __block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url]; 
    [request setDelegate:self]; 
    [request setCompletionBlock:^{ 
     NSString *response=[request responseString]; 
     NSLog(@"%@",response); 


    }]; 
    [request setFailedBlock:^{ 

    }]; 
    [request startAsynchronous];*/ 

} 
-(IBAction)switchSwitched:(id)sender{ 
    if(onOffline.on){ 
     [label1 setText:@"For Hire"]; 
     [label1 setTextColor:[UIColor colorWithRed:0.0 green:0.8 blue:0.0 alpha:1.0]]; 

     uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(uploadGPS_tick:) userInfo:nil repeats:YES]; 
     [self uploadGPS_tick:nil]; 
    }else{ 
     [label1 setText:@"Engaged"]; 
     [label1 setTextColor:[UIColor colorWithRed:0.8 green:0.0 blue:0.0 alpha:1.0]]; 

     if([uploadGPS_timer isValid]){ 
      [uploadGPS_timer invalidate]; 
     } 
    } 

} 
-(void)uploadGPS_tick:(NSTimer*)timer{ 
    if(!lat || !lng){ 
     //do nothing 
    }else{ 
     NSString *urlStr=[[NSString alloc] initWithFormat:@"http://www.prestocab.com/driver/ajax/updateCoords.php"]; 
     NSURL *url=[NSURL URLWithString:urlStr]; 

     __block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url]; 
     [request setPostValue:lat forKey:@"lat"]; 
     [request setPostValue:lng forKey:@"lng"]; 
     NSLog(@"EOH: %@",lat); 
     [request setDelegate:self]; 
     [request setCompletionBlock:^{ 
      NSString *response=[request responseString]; 
      NSLog(@"%@",response); 
      //do nothing 
     }]; 
     [request setFailedBlock:^{ 
      //NSError *error =[request error]; 
      //do nothing 
     }]; 
     [request startAsynchronous]; 
    } 
} 

-(void)locationManager:(CLLocationManager*) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation*) oldLocation{ 
    lat=[[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.latitude]; 
    lng=[[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.longitude]; 
    NSLog(@"%@,%@",lat,lng); 
} 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [email protected]"PrestoCab"; 
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(logout:)];  
    self.navigationItem.rightBarButtonItem = anotherButton; 
    [anotherButton release]; 


    //GPS 
    lm=[[CLLocationManager alloc] init]; 
    if([CLLocationManager locationServicesEnabled]){ 
     lm.delegate=self; 
     lm.desiredAccuracy=kCLLocationAccuracyBest; 
     lm.distanceFilter=30.0f; 
     [lm startUpdatingLocation]; 
    } 
    //[self check4messages_tick:nil]; //want to start immediately, not in 10/40 seconds' time 
    [self uploadGPS_tick:nil]; 
    //check4messages_timer=[NSTimer scheduledTimerWithTimeInterval:40.0 target:self selector:@selector(check4messages_tick:) userInfo:nil repeats:YES]; 
    uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(uploadGPS_tick:) userInfo:nil repeats:YES]; 



    [super viewDidLoad]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    if([uploadGPS_timer isValid]){ 
     [uploadGPS_timer invalidate]; 
    } 
} 
-(void)dealloc{ 
    [super dealloc]; 
    [uploadGPS_timer release]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

另外,這裏是

MyClassViewController.h:

#import <UIKit/UIKit.h> 

@interface MyClassViewController : UINavigationController{ 
    IBOutlet UIButton *passenger; 
    IBOutlet UIButton *driver; 

} 
@property (nonatomic,retain) UIButton *passenger; 
@property (nonatomic,retain) UIButton *driver; 

-(IBAction) passengerClicked:(id)sender; 
-(IBAction) driverClicked:(id)sender; 

@end 

和MyClassViewController.m:

#import "MyClassViewController.h" 
#import "MainMenuDriver.h" 

@implementation MyClassViewController 
@synthesize passenger; 
@synthesize driver; 

-(IBAction)passengerClicked:(id)sender{ 
    NSLog(@"passenger"); 
} 
-(IBAction)driverClicked:(id)sender{ 
    NSLog(@"driver"); 
    MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil]; 
    [[self navigationController]pushViewController:mainMenuDriver animated:YES]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 
+1

MainMenuDriver是一個視圖控制器嗎?請確保... – Maulik

+0

感謝您的評論 - 這是從MainMenuDriver.h:@interface MainMenuDriver:UIViewController { – Eamorr

+0

您是否應該爲nib名稱放置'nil'?它將如何知道要顯示的內容? – JoshRagem

回答

2

我可以看到幾件事情:

  1. 你的筆尖文件的名字從你的類的名稱不同。如果你把零,UIViewController將嘗試加載一個nip文件與UIViewController的名字。您的UIViewController可以是MainMenuDriver,但您的筆尖文件名可以是MainMenuDriverNibFileName.nib
  2. [self navigationController]nil

爲此,請執行下列操作:

NSLog(@"hi"); 
    if([self navigationController]){ 
     MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil]; 
     [[self navigationController]pushViewController:mainMenuDriver animated:YES]; 
    } 
    else{ 
     NSLog(@"Houston we have a problem"); 
    } 

更新1:

因此,它nil,你可以做的是:

  1. 快速和髒:

    [self presentViewController:mainMenuDriver animated:YES];

  2. 而不是UIViewController切換到UINavigationController

+0

我不明白你的第一點! – Maulik

+1

很公平,但是,我會保持因爲其他人可能會覺得它很有用,很容易理解我的觀點,用一個'UIViewController'創建一個虛擬項目,並通過放置你的nib文件的名字來顯示它,然後把它放到'nil' ,它仍然會工作...現在將項目導航器上的文件名更改爲其他內容,同時仍然創建'UIViewController',將其筆尖名稱設置爲'nil'。 – Peres

+0

@JackyBoy「我得到了休斯頓,我們遇到了問題「任何想法接下來要做什麼? – Eamorr