使用 UIButton 转换 UIView 时出现问题

发布于 2024-12-08 06:53:41 字数 3353 浏览 0 评论 0原文

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

我想做的就是通过 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.'

我很想知道我错过了什么。

I am a very inexperienced developer and have run into a problem that should be easy however after hours of reading over Apple's Developer guide and many different questions on this site I am still stumped.

All I am trying to do is transition from my root view controller to my next view controller via a UIButton.

Here is 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

Important stuff from 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

And finally, 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

Here is the error:

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.'

I would love to know what I missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城之時有危險 2024-12-15 06:53:41

您在推送之前已经发布了 dailyDealViewController。将release语句移至pushViewController语句之后。

You have released dailyDealViewController before pushing it. Move the release statement to after the pushViewController statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文