2012-03-13 28 views
0

我需要做這樣的標籤:轉到到另一個函數

-(void)function1{ 
goto start; 
} 

-(void)function2{ 
//some code 
start://i need to get in here exactly, [self function2] oblige me to execute the function2 from the beginning 
//some code.. 
} 

看來我不能,我能做些什麼呢? thanx提前。

編輯:這裏是我的實際代碼:

-(void)viewWillAppear:(BOOL)animated{ 
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
    { 
     NSString *querySQL = [NSString stringWithFormat: 
           @"Select quiz,question,p1,p2,p3,num_correct from question where quiz=(select id from quiz where nom = \'%@\' and niveau= \'%i\' and theme=(select id from theme where nom=\'%@\'))",nomPartieQuiz,quiIslam.theGameLevel,quiIslam.themeChoisie]; 

     const char *query_stmt = [querySQL UTF8String]; 

     if (sqlite3_prepare_v2(contactDB, 
           query_stmt, -1, &statement, NULL) == SQLITE_OK) 
     { 
     start://étiquette 
      while (sqlite3_step(statement) == SQLITE_ROW && !passToNextQuestion) 
      { 



       NSString *addressField = [[NSString alloc] 
              initWithUTF8String: 
              (const char *) sqlite3_column_text(
                      statement, 1)]; 

       NSLog(@"%@",addressField); 
       txtView.text=addressField; 

       passToNextQuestion=NO;         

      } 

      sqlite3_finalize(statement); 
     } 
     sqlite3_close(contactDB); 
    } 

} 


-(IBAction)clickOnTheButton:(id)sender{ 

    btn1.hidden=YES; 
    goto start; 
} 
+0

你能發佈你的實際代碼,所以我們可以看到使用goto背後的推理嗎? – 2012-03-13 13:59:36

+0

編輯,請看看:) – Luca 2012-03-13 14:02:50

回答

6

試試這個:

-(void)function1{ 
[self function2]; 
} 

-(void)function2{ 

//some code.. 
} 

,並利用其只允許在的情況下,一個非常非常非常特別親切 「的goto」你現在應該忘記他們!它再次遵循任何發展原則。

+0

我不需要去'function2',我需要去'function2'中的特定行 – Luca 2012-03-13 13:58:26

+2

如果您需要在代碼中'轉到'任何特定的行,你已經搞得很糟糕了。正如這個答案所說的,你應該將你在goto中執行的任何代碼移動到一個方法中,然後調用它,而不是跳出當前的方法。如果你不想在方法後執行更多的邏輯,只需調用return;並退出該方法。 – 2012-03-13 14:00:44

+0

完全同意,只是審查你的代碼,有一個不同的做事方式比GOTO,真的,我一直在開發多年,我從來沒有使用它,當然你不需要! – 2012-03-13 14:02:49

2

關於你的編輯,把開始的邏輯:放到它自己的方法中,然後從你的viewWillAppear和你需要執行它的任何其他地方調用該方法。請記住,您可能需要創建一些可變的全局變量,以便您的方法可以看到它們,但即使您必須修改執行方式,也是這樣。

-(void)sqlStartLogic { 
while (sqlite3_step(statement) == SQLITE_ROW && !passToNextQuestion) 
    { 
       NSString *addressField = [[NSString alloc] 
              initWithUTF8String: 
              (const char *) sqlite3_column_text(statement, 1)]; 
     NSLog(@"%@",addressField); 
     txtView.text=addressField; 

     passToNextQuestion=NO;         
    } 

    sqlite3_finalize(statement); 
} 

-(void)function2 { 
    [self sqlStartLogic]; 
}