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