EXC_BAD_ACCESS,而textField则为FirstFirstResponder

时间:2014-08-14 23:07:58

标签: ios objective-c

当我点击我EXE_BAD_ACCESS back NavigationBar上的CreateViewController按钮pushed <UINavigationController < / p>

当我在CreateViewController.m中启用以下行时(底部的完整代码)

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    //[self.textField becomeFirstResponder];
}

我开始 Thread1:EXE_BAD_ACCESS(code=1, address=...)

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

如果我禁用上面的代码行,那么按下后退按钮会按预期将我带到MainViewController。

我是iOS的新手。我做错了什么?请参阅下面的代码。


Appdelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

Appdelegate.m

import "AppDelegate.h"
import "MainViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    id controller  = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
    id navController = [[UINavigationController alloc] initWithRootViewController:controller];

    self.window.rootViewController = navController;

    [self.window makeKeyAndVisible];
    return YES;
}

@end

MainViewController.h

@interface MainViewController : UIViewController
- (IBAction)create:(id)sender;

@end

MainViewController.m

import "MainViewController.h"
import "CreateViewController.h"

@implementation MainViewController

#pragma mark - View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
}

- (IBAction)create:(id)sender {

    id controller = [[CreateViewController alloc] initWithNibName:@"CreateView" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}
@end

CreateView.h

@interface CreateViewController : UIViewController <UITextFieldDelegate>

@end

CreateView.m

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setTintColor:[UIColor purpleColor]];
    self.textField=[[UITextField alloc] initWithFrame:CGRectMake(80, 10, 160, 30)];
    [self.textField setBorderStyle:UITextBorderStyleRoundedRect];
    self.textField.placeholder = @"Name";
    [self.textField setBackgroundColor:[UIColor clearColor]];
    self.textField.delegate = self;

    [self.navigationController.navigationBar addSubview:self.textField];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    //[self.textField becomeFirstResponder];
}

2 个答案:

答案 0 :(得分:0)

转到断点导航器并添加异常断点,如下所示,第一个选项:enter image description here

答案 1 :(得分:0)

不是将self.textField作为子视图添加到导航栏,而是将textField设置为视图控制器导航项的titleView。

    [self.navigationItem setTitleView:self.textField];

这必须有帮助......