2012-06-17 36 views
0
CGRect buttonFrame = CGRectMake(10, 80, 100, 30); 
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame]; 
[button setTitle: @"Start" forState: UIControlStateNormal]; 
[button addTarget:self action:@selector(start_Accel) forControlEvents:UIControlEventTouchUpInside]; 
[button setTitle: @"Stop" forState: UIControlEventTouchUpInside]; 
[button addTarget:self action:@selector(stop_Accel) forControlEvents:UIControlEventTouchUpInside]; 
[view addSubview:button]; 

我想創建一個按鈕,可以在用戶按下時啓動和停止加速度計更新,但它的標題也應該從開始到停止更改,並且它應該繼續,直到用戶通過anoterh按鈕重置所有內容,但我有除了下面的代碼,沒有想法完成它,但它不會改變標題並呼叫停止操作?創建一個可以觸發兩個動作並更改標題的按鈕?

預先感謝您!

回答

4

管理某種類型的按鈕的狀態例如。用@property和使用只有一個動作:

在您在您的m的.h

@property (nonatomic) BOOL startStopButtonIsActive; 

@synthesize startStopButtonIsActive = _startStopButtonIsActive; 

.... 

- (void)viewDidLoad { 
     [button addTarget:self action:@selector(startStopButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 
     // other stuff 
} 

- (void)startStopButtonPressed { 
    if (startStopButtonIsActive) { 
     self.startStopButtonIsActive = !self.startStopButtonIsActive //toggle! 
     // do your stuff here 
    } else { 
     self.startStopButtonIsActive = !self.startStopButtonIsActive //toggle! 
     // do your stuff here 
    } 
} 
+0

謝謝你的回答,在這種情況下,我可以改變按鈕的文字?我需要在.h中定義按鈕@property(保留)IBOutlet UIButton * someButton; 然後使用[somebutton setTitle:@「Start」forState:UIControlStateNormal]; – sam

+0

是的,這是正確的! – Pfitz

+0

嗨。我可以知道你爲什麼用@synthesize startStopButtonIsActive = _startStopButtonIsActive編輯你的答案的原因; 也是我能夠實現代碼來改變標題,謝謝! – sam

0

應該只有在這種情況下一個目標,你應該有它通過添加參數給簽名傳遞按鈕以及動作:

[button addTarget:self action:@selector(accelButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
// the interface being: 
-(void) accelButtonPressed:(UIButton *) sender; 

在該方法中,您可以將c heck狀態和更改狀態,以及按鈕的屬性(在'sender'中引用該屬性。

0

首先你要在.h文件中聲明你的按鈕和一個整型變量。

然後,

intergervariable=0; 

    button = [[UIButton alloc] initWithFrame: buttonFrame]; 
    [button setTitle: @"Start" forState: UIControlStateNormal]; 
    [button addTarget:self action:@selector(start_Accel) forControlEvents:UIControlEventTouchUpInside]; 
    [view addSubview:button]; 

after that inside the start_Accel function check like this 


if(intergervariable==0) 
{ 
intergervariable=1; 
[button setTitle: @"Start" forState: UIControlStateNormal]; 
} 
else if(intergervariable==1) 
{ 
intergervariable=0; 
[button setTitle: @"Stop" forState: UIControlStateNormal]; 
} 
1

你接近。

只添加一個目標到按鈕 - 這樣目標方法就知道了當前狀態。確保在選擇器中有一個冒號,以便它接收發件人作爲方法的參數。即startOrStopAccel:startOrStopAccel

根據狀態,您可以使用一個目標方法調用start_Accelstop_Accel

-(void) viewDidLoad { 
    //... 
    //... 
    CGRect buttonFrame = CGRectMake(10, 80, 100, 30); 
    UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame]; 
    [button setTitle: @"Start" forState: UIControlStateNormal]; 
    [button addTarget:self action:@selector(startOrStopAccel:) forControlEvents:UIControlEventTouchUpInside]; 
    [view addSubview:button]; 
} 

-(void)startOrStopAccel:(UIButton*)sender { 
    if ([sender.title isEqualToString:@"Start"]) { 
     [sender setTitle: @"Stop" forState: UIControlStateNormal]; 
     [self start_Accel]; 
    } 
    else { 
     [sender setTitle: @"Start" forState: UIControlStateNormal]; 
     [self stop_Accel]; 
    } 
} 
相關問題