简体   繁体   English

使用分配的IBAction(UIButton)点按IBOutlet后,iOS应用程序崩溃无任何异常

[英]iOS app crashes without any exception after tapping IBOutlet with assigned IBAction (UIButton)

My task is to create custom UIAlertView with multiline text input field and everything works pretty much well up to the point where I need to do some actions if dismiss button was tapped. 我的任务是使用多行文本输入字段创建自定义UIAlertView ,一切正常,直到点按“关闭”按钮时我需要执行一些操作。 As example I'm using: http://iphonedevelopment.blogspot.co.uk/2010/05/custom-alert-views.html 例如,我正在使用: http : //iphonedevelopment.blogspot.co.uk/2010/05/custom-alert-views.html

I have created very simple popup for testing purpose. 我已经创建了非常简单的弹出窗口用于测试。 Basically it's UIViewController xib with single button in it. 基本上是带有单个按钮的UIViewController xib。 The main class is demonstrated below: 主要类别如下所示:

#import "testViewController.h"

@interface testViewController ()

@end

@implementation testViewController

- (id)init
{
    self = [super initWithNibName:@"testViewController" bundle:nil];

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showInView:(UIView *)view
{
    [view addSubview:[self view]];
    [[self view] setFrame: [view bounds]];
    [[self view] setCenter:[view center]];
}

- (IBAction)onTap:(id)sender {
    NSLog(@"All OK");
}

@end

then in root UIViewController I'm calling my custom alert: 然后在根UIViewController调用我的自定义警报:

- (IBAction)showAlert:(id)sender
{
    dispatch_async(dispatch_get_main_queue(), ^{
        testViewController *alert = [[testViewController alloc] init];
        [alert showInView:[self view]];
    });
}

So far I have tried Main thread or global queue or even synchronous dispatch, but everything ends up with break on: 到目前为止,我已经尝试了主线程或全局队列,甚至是同步分派,但是一切都以断断续续而告终:

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); <-- Thread 1:EXC_BAD_ACCESS (code=1, address=0xa000000c)
    }
}

tried to add observer for the button but still no go.. 试图为按钮添加观察者,但还是没有。

Any help is much appreciated 任何帮助深表感谢

Check if your UIButton is assigned multiple IBoutlets or multiple IBActions. 检查您的UIButton是否被分配了多个IBoutlet或多个IBAction。 This a common issue which happens without our notice sometimes. 这是一个常见问题,有时会在不通知我们的情况下发生。

事实证明,我的testViewController是在我点击按钮之前由ARC发布的

Here what happing is "if you want to add one viewcontroller view to other viewcontroller's view then after adding of views you should convey to the compiler that second view controller is going to be child to the first" 这里的含义是“如果您想将一个viewcontroller视图添加到其他viewcontroller的视图中,那么在添加视图之后,您应该向编译器传达第二个视图控制器将成为第一个视图控制器的子视图”

ie pre intimation of parent to child relationship has to be done. 即必须事先告知父母与子女的关系。

now just modify your showAlert method with the following . 现在只需使用以下命令修改showAlert方法。 it is working 100%. 它正在100%工作。

- (IBAction)showAlert:(id)sender {

    dispatch_async(dispatch_get_main_queue(), ^{
        testViewController *alert = [[testViewController alloc] init];
        [alert showInView:[self view]];

        [self addChildViewController:alert];
        [alert didMoveToParentViewController:self];
    });
}

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

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