1

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
{
    self.navigationItem.title=@"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
4

1 に答える 1

2

私はいくつかのことを見ることができます:

  1. nib ファイルの名前は、クラスの名前とは異なります。nil を指定すると、 はの名前のUIViewControllernib ファイルをロードしようとします。UIViewControllerあなたはそうすることがUIViewControllerできますMainMenuDriverが、あなたのnibファイル名は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 animation:YES];

  2. UIViewControllerに切り替える代わりに、 UINavigationController.

于 2012-06-26T13:21:17.677 に答える