簡體   English   中英

EXC_BAD_ACCESS,而textField成為FirstResponder

[英]EXC_BAD_ACCESS while textField becomeFirstResponder

當我在UINavigationControllerpushed CreateViewController NavigationBar上單擊“ back ”按鈕時,出現EXE_BAD_ACCESS異常

當我在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];
}

轉到斷點導航器並添加異常斷點,如下所示,第一個選項: 在此處輸入圖片說明

無需將self.textField作為子視圖添加到導航欄,而是將textField設置為視圖控制器的導航項的titleView。

    [self.navigationItem setTitleView:self.textField];

這必須有幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM