簡體   English   中英

自定義類中的UIButton目標操作

[英]UIButton target action inside custom class

我一直在努力解決一個錯誤,並且找到了一種解決方法,但是我想了解到底發生了什么。 它與子類內部的UIButton目標操作根據不同的子視圖層次結構而觸發失敗有關。

簡介:我有一個NSObject的子類,帶有一個UIView屬性對象,一個附加的UIButton,以及一個添加到該按鈕的目標,該目標調用該子類內部的函數。 在主ViewController內,我初始化該子類並將其視圖添加到視圖堆棧中,單擊按鈕,它使我進入main.mm並出現錯誤-EXC_BAD_ACCESS,給我的反饋很少。 因此層次結構如下所示:

-CustomClass
--UIView           <-this is added as a subview to the View Controller
---UIButton (onRelease calling a function)

因此,我通過將自定義類更改為UIView的子類而不是NSObject進行了修復,然后將其@property UIView添加為自定義類的子視圖(並且按鈕仍附加到該子視圖),然后在主菜單中在View Controller中,我將自定義類本身添加為子視圖,而不是該類的subview屬性對象。 然后該按鈕成功調用該函數。 因此新的安排如下所示:

-CustomClass (now UIView)     <-this is added as a subview to the View Controller
--UIView                      <-this is added as a subview to CustomClass
---UIButton (onRelease calling a function)

然后,我意識到我可以僅將CustomClass保留為兩個實例的UIView的子類,如果其他所有條件不變,則問題仍然存在於原始設置中。

好的,更多細節,代碼如下:

CustomClass:.h

@interface Temp : UIView
@property UIView *subview;
@property UIButton *but;
@end

.m

-(id) init{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if(self){
        _subview = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        //[self addSubview:_subview];  // FOR THE FIX
        _but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_but setTitle:@"OKAY" forState:UIControlStateNormal];
        [_but setBackgroundColor:[UIColor blackColor]];
        [_but setFrame:CGRectMake(50, 50, 200, 200)];
        [_subview addSubview:_but];
        [_but addTarget:self action:@selector(pageTurn) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void) pageTurn{
    NSLog(@"WORKS");
}

內部視圖控制器:

Temp *temp = [[Temp alloc] init];
[self.view addSubview:temp.subview];
//[self.view addSubview:temp];  // FOR THE FIX, instead of above line

您將不必要的各種視圖層次結構東西弄亂了,這很可能是問題的原因。 我創建了一個Test UIView子類,該子類具有一個UIButton實例變量,該變量作為子視圖添加到Test對象中,無需將另一個視圖添加為子視圖,然后將按鈕添加到子視圖中,然后在視圖控制器中添加按鈕的子視圖到視圖-它比需要的復雜得多。

簡而言之-創建Temp UIView,將按鈕添加為子視圖,然后在視圖控制器類中將Temp UIView添加為子視圖。 就這么簡單,下面是代碼:

- (id)init {
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if(self){
        _but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_but setTitle:@"OKAY" forState:UIControlStateNormal];
        [_but setBackgroundColor:[UIColor blackColor]];
        [_but setFrame:CGRectMake(100, 100, 200, 200)];
        [_but addTarget:self action:@selector(pageTurn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_but];
    }
    return self;
}

- (void)pageTurn {
    NSLog(@"WORKS");
}

然后將一個實例添加到我的視圖控制器中:

Test *temp = [[Test alloc] init];
temp.backgroundColor = [UIColor redColor];
[self.view addSubview:temp];

結果是:

在此處輸入圖片說明

誰在堅持temp

如果沒有任何人引用temp ,那么它將被釋放。 那時目標是僵屍,您當然會崩潰。 temp.subview正在舉行self.view

在第二個設置中,將temp添加為self.view的子視圖可保留對其的引用。


您可以通過在視圖控制器中添加Temp *屬性來解決此問題。

self.temp = [[Temp alloc] init];
[self.view addSubview:self.temp.subview];

暫無
暫無

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

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