尝试使用UIButton转换UIViews

时间:2011-10-04 04:55:49

标签: iphone uiview uibutton

我是一个非常缺乏经验的开发人员,并且遇到了一个应该很容易的问题,但是经过几个小时阅读Apple的开发人员指南和本网站上的许多不同问题后,我仍然感到难过。

我要做的就是通过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];


    [self.navigationController pushViewController:dailyDealViewController animated:YES];

    [dailyDealViewController release];
}
@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.'

我很想知道我错过了什么: - /

非常感谢你。

1 个答案:

答案 0 :(得分:1)

从它的外观来看

这应该是IBOutlet

IBOutlet UIButton *cityButton;

并且应该在您的xib文件中链接,方法是右键单击First Responder并将链接拖到导航栏中的按钮。