2012-09-01 61 views
1

看起來像這樣應該是關於你可以用int做的最簡單的事情,但我似乎找不到任何顯示如何去做的事情。試過谷歌,但我得到了百萬次點擊,所有這些都比我試圖理解的更深入。如何在Xcode中添加一個int

我想要做的是有一個整數和發生的事情(按下按鈕,圖像移動,無論)我想添加1到這個整數,並有一個單獨的方法,當它的調用,檢查整數和預成形一取決於價值的幾個行動。

我知道如何做到這一點,除了加1到int?

我知道這對你們來說肯定是一個瘋狂的愚蠢問題,有人在乎我把骨頭扔了嗎?

回答

2

申報INT指數:

-(void)btnClicked:(id)sender 
{ 
    index++; 
} 

任何其他方法圖像移動則

index++; 

現在這個樣子執行操作:

-(void)performAction:(int)index 
{ 
    switch(index) 
    { 
     //make case depending on number 
     case : 1 
     { 
      // do something here on index 1 
     } 
     break; 
     ..... 
     ..... 
     ..... 
     default 
     { 
      // do something here if not that index 
     } 
     break; 

    } 


} 
+0

價格,你搖滾的朋友!索引++;爲我做了這件事,我試圖做index + 1;交換機部分也將幫助我清理我打算使用的所有if語句。謝啦! – DelightedD0D

2
int i = 4; 
i++; 

這是你在找什麼?

+0

是的,正是這樣,標記上面的答案被接受,因爲它可能會幫助更多的人在路上,但你釘了它 – DelightedD0D

0

你應該添加一個NSUInteger屬性到UIViewController中你想要計算的行爲。然後,作爲每個操作的第一步,您可以將1添加到該屬性。在.h文件中

現在就點擊按鈕

@interface MyViewController extends UIViewController 
@property (nonatomic) NSUInteger actionCounter; 
@end 

@implementation MyViewController 
@synthesize actionCounter; 

//in your actions here, add one to actionCounter 
-(void) IBAction doSomething{ 
    self.actionCounter++; 

    //do the actual action here 
} 

//in your check method, read the actionCounter value and do something 
-(void) checkActionCount{ 
    if(self.actionCounter => 1){ 
     NSLog(@"User did something"); 
    } 
} 

@end