2015-06-26 19 views
0

我想用這裏的功能塊替換「@selector」。但是,這樣做顯示了一個錯誤說:發送void(^)(UIButton * _strong)到不兼容類型的參數時出錯SEL

錯誤:發送「無效(^)(UIButton的* __強)」不兼容的型「SEL」的參數

請就如何把一個函數指示直接阻止,而不是使用「@選擇器」。

[_contactPhoneButton addTarget:self 
         action:^(UIButton *sender) { 
    NSString *phoneNumber = [@"tel://" stringByAppendingString:sender.titleLabel.text]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]]; 
} 

回答

1

你不能用一個Block替換Selector因爲他們是不同類型的。然而,你可以看看如何使用ReactiveCocoa這樣的東西來實現你想要的方式。

Reactive Cocoa

1

您必須添加選擇,而不是塊:

[_contactPhoneButton addTarget:self 
         action: @selector(buttonTapped:)]; 

然後

- (void) buttonTapped: (uibutton*) sender 
{ 
NSString *phoneNumber = [@"tel://" stringByAppendingString:sender.titleLabel.text]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]]; 
} 
0

試試這個:

[_contactPhoneButton addTarget:[^{ 
     NSString *phoneNumber = [@"tel://" stringByAppendingString:sender.titleLabel.text]; 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]]; 
    } copy] action:@selector(invoke) forControlEvents:UIControlEventTouchUpInside]; 
+0

私有方法,保留週期。不是一個好建議。 –

2

塊不能被轉換爲SEL 。他們是不同的類型。

  • SEL是一個名稱來標識的功能,它是發送到object.During運行時,對象使用SEL找到函數來執行
  • 座是一些代碼就可以執行,它是獨立的對象。

如果您還想使用塊,請參閱此link

相關問題