简体   繁体   English

EXC_BAD_ACCESS,而textField成为FirstResponder

[英]EXC_BAD_ACCESS while textField becomeFirstResponder

I am getting EXE_BAD_ACCESS exception when I hit " back " button on NavigationBar of my CreateViewController which is pushed on UINavigationController 当我在UINavigationControllerpushed CreateViewController NavigationBar上单击“ back ”按钮时,出现EXE_BAD_ACCESS异常

When I enable following line in CreateViewController.m (full code at the bottom) 当我在CreateViewController.m启用以下行时(底部的完整代码)

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

I start getting Thread1:EXE_BAD_ACCESS(code=1, address=...) 我开始得到Thread1:EXE_BAD_ACCESS(code=1, address=...)

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

If I keep the above line of code disabled, then hitting back button takes me to MainViewController as expected. 如果我禁用了上面的代码行,则按“后退”按钮将我带到MainViewController,正如预期的那样。

I am a newbie to iOS. 我是iOS的新手。 What I am doing wrong? 我做错了什么? Please see my code below. 请在下面查看我的代码。


Appdelegate.h Appdelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

Appdelegate.m 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 MainViewController.h

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

@end

MainViewController.m 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 CreateView.h

@interface CreateViewController : UIViewController <UITextFieldDelegate>

@end

CreateView.m 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];
}

Go to breakpoint navigator and add exceptions breakpoint as shown below , 1st option : 转到断点导航器并添加异常断点,如下所示,第一个选项: 在此处输入图片说明

Instead of adding self.textField as subview to navigation bar, set the textField as titleView of navigation item of the view controller. 无需将self.textField作为子视图添加到导航栏,而是将textField设置为视图控制器的导航项的titleView。

    [self.navigationItem setTitleView:self.textField];

This must help... 这必须有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM