(目标:对象)不适用于从外部viewController设置UIButton [英] (Target: Object) not working for setting UIButton from outside viewController

查看:49
本文介绍了(目标:对象)不适用于从外部viewController设置UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以创建UIButton.
但是callback:target:对象在当前对象中不起作用.??
我只能执行"self.viewController"对象,而不能执行当前对象的"self".

I can create the UIButton.
But the callback: target: object won't work within the current object. ??
I can only do "self.viewController" object, and can't do "self" for current object.

thx

#import <Foundation/Foundation.h>
#import "ViewController.h"

@class ViewController;

@interface CGuiSetup : NSObject
{
    @public
    ViewController *viewController;
}
- (void) Setup;
- (void) ButtonRespond:(UIButton*) btn;
@end


#import "CGuiSetup.h"

@implementation CGuiSetup

- (void) ButtonRespond:(UIButton*) btn
{
    NSLog(@"ButtonRespond");
}


- (void) Setup
{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setFrame:CGRectMake(10,10,100,100)];

    // But I want to call the following in the CGuiSetup object (self) instead... but it crashes out if I leave it as just "self"
    [btn addTarget:self.viewController action:@selector(ButtonRespond:)
        forControlEvents:UIControlEventTouchUpInside]; //works: for "self.viewController" if I put ButtonRespond in the ViewController

    [btn addTarget:self action:@selector(ButtonRespond:)
        forControlEvents:UIControlEventTouchUpInside]; //fails: for "self"

    [self.viewController.view addSubview:btn];
}
@end


#import <UIKit/UIKit.h>
#import "CGuiSetup.h"

@class CGuiSetup;

@interface ViewController : UIViewController
{
    CGuiSetup *guiSetup; //<---- had to take this out of the "viewDidLoad" method
}
@end


- (void)viewDidLoad
{
    [super viewDidLoad];

    guiSetup = [CGuiSetup alloc];
    guiSetup->viewController = self;


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setFrame:CGRectMake(10,10,100,100)];
    [btn addTarget:guiSetup action:@selector(ButtonRespond:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

推荐答案

如果使用的是ARC,是否有任何对象具有对CGuiSetup的保留引用?我听起来好像CGuiSetup被实例化,创建(或可能从另一个对象接收)viewController,向其添加按钮,然后将视图控制器提供给另一个对象(也许通过将其推入navController或将其设置为的根控制器)该应用程序)?无论是哪种情况,都将CGuiSetup取消分配,并且该按钮尝试将消息发送到已被破坏的对象.如何/在哪里创建CGuiSetup?哪个对象保留对其的引用?那可能就是您的问题所在.

If you're using ARC, does any object have a retaining reference to CGuiSetup? I sounds like CGuiSetup is instantiated, creates (or maybe receives from another object) the viewController, adds the button to it and then gives the view controller to another object (perhaps by pushing it on a navController or setting it to be the root controller of the app)? Whatever the case is, CGuiSetup it being dealloc'd and the button is trying to send a message to an object that's already been destroyed. How/where is CGuiSetup created? What object retains a reference to it? That's probably where your problem is.

如果您不了解保留/释放的内容和/或不知道ARC是什么,则需要阅读Apple的内存管理指南:

If you don't understand what retain/release is and/or you don't know what ARC is, you need to read Apple's memory management guide: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

这篇关于(目标:对象)不适用于从外部viewController设置UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆