0
我是一个非常没经验的开发人员,遇到了一个应该很容易的问题,但是在Apple的开发人员指南上阅读了数小时后,本网站上的许多不同问题仍然难以理解。使用UIButton转换UIViews时遇到困难
我想要做的就是通过UIButton从我的根视图控制器转换到我的下一个视图控制器。
这里是appdelegate.h:
#import <UIKit/UIKit.h>
@class MainViewController;
@interface MDAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
从Appdelegate.m重要的东西:
#import "MDAppDelegate.h"
#import "MainViewController.h"
@implementation MDAppDelegate
@synthesize window, navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
MainViewController.h:
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController {
UIButton *cityButton;
}
@property (nonatomic, retain) IBOutlet UIButton *cityButton;
- (IBAction)chooseCityAction:(id)sender;
@end
最后,MainViewController.m:
#import "MainViewController.h"
#import "DailyDealViewController.h"
@implementation MainViewController
@synthesize cityButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)dealloc
{
[cityButton release];
[super dealloc];
}
- (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
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
}
- (void)viewDidUnload
{
[self setCityButton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)chooseCityAction:(id)sender {
DailyDealViewController *dailyDealViewController = [[DailyDealViewController alloc]initWithNibName:@"DailyDealViewController" bundle:nil];
[dailyDealViewController release];
[self.navigationController pushViewController:dailyDealViewController animated:YES];
}
@end
以下是错误:
2011-10-03 21:56:14.258 MD[4235:207] *** Terminating app due to uncaught exception
'NSUnknownKeyException', reason: '[<UIViewController 0x4b434c0>
setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
cityButton.'
我很想知道我错过了什么。
谢谢Akshay,但我实际上有这样的想法,我被给出了同样的错误! – jisturiz
那么,发布声明将在稍后发布。所以,保持这种方式。是否已将按钮连接到“界面”构建器中的操作? – Akshay
很酷,我把它改回来,它会离开它。 – jisturiz