gpt4 book ai didi

iPhone Objective C - 无法显示新 View

转载 作者:行者123 更新时间:2023-11-29 11:11:51 25 4
gpt4 key购买 nike

我发现开始使用 Objective-C 很困难。

单击按钮时我正在执行以下代码:

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

当我按下按钮时,我可以在控制台中看到“hi”,只是 View 应该更改为 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

最佳答案

我可以看到一些东西:

  1. 您的 nib 文件名与您的类名不同。如果你输入 nil,UIViewController 将尝试加载一个带有 UIViewController 名称的 nib 文件。您的 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 animated:YES];

  2. UIViewController 切换到 UINavigationController

关于iPhone Objective C - 无法显示新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11208053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com