2012-10-17 40 views
1

我對iPhone的開發很陌生。我試圖禁用已經存在的按鈕,但我實際上不能獲得指向視圖中特定元素的指針。舉例來說,我在的viewController頭從viewDidLoad獲取UIButton引用

- (IBAction)one:(id)sender; 

以下且實現

- (IBAction)one:(id)sender { 

}

這僅僅是事件處理程序。但是,當視圖打開時,我需要禁用按鈕,並且我對如何獲取對事件處理程序外部元素的引用有點遺失。

因此,換句話說,我的想法是有這樣的:

UIButton* myButton = //something 

,其中一些就是我迷失在做什麼。有任何想法嗎?我非常感謝我在這裏得到的任何幫助!

+0

一種方法是將標籤設置爲按鈕特定的事件並在需要時進行更改。 – iCreative

+0

從按鈕的屬性界面,使其隱藏 –

+0

是您的問題解決? – Rajneesh071

回答

5

您需要爲您的界面按鈕創建一個屬性:

@property(nonatomic, retain) IBOutlet UIButton * button; 

這增加了實施:

@synthesize button; 

然後將按鈕連接到它在界面生成器。在此之後,你可以通過禁用按鈕:

button.enabled = NO; 

希望我能幫忙!

+0

不幸的是,我不能選擇你的答案几分鐘,但有一件事要補充到這將是你需要@synthesize變量的實現。非常感謝你! –

+0

你說得對,我編輯了我的答案。很高興我能幫上忙! – romeouald

0

在您的.h文件中

#import <UIKit/UIKit.h> 

@interface RpViewController : UIViewController 

@property (retain , nonatomic)IBOutlet UIButton *Btn1; 

@end 

在您.m文件,實施這樣寫:

@synthesize Btn1; 

Now on interface , click on button. 
In button's properties - > Drawings - check Hidden checkbox. 

Wherever you want to show that button , just write. 



[Btn1 setHidden:FALSE]; 
1

只要給標記您的按鈕,並與標籤訪問按鈕值。

UIButton *btn = (UIButton*)[self.view viewWithTag:1]; 
[btn setHidden:YES]; 
+1

Perfec,謝謝! – PeeS

0
@property (strong, nonatomic) UIButton *button; 
@synthesize button; 

// In View Did Load... 
self.button = [UIButton buttonWithType:UIButtonTypeCustom]; // button can be of any type. 
[self.button setTag:1]; 
// if you have more buttons initialize it and set its tag. you can get to know which button was pressed using tags. 

[button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside]; 

-(void) buttonEvent:(UIButton *) sender 
{ 
    NSLog(@"%d",sender.tag); 
    if(sender.tag == 1) 
    { 
     [self.button setEnabled:NO]; // This makes your button disabled, i.e you can see the button but you cannot click on it. 
     [self.button setHidden:YES]; // This makes your button hidden. 
    } 
} 

如果你有更多的疑惑ping命令我回去。