转到另一个功能的标签

时间:2012-03-13 13:51:45

标签: objective-c ios

我需要这样做:

-(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..
}

似乎我不能,我该怎么办呢?提前完成。

编辑:这是我的实际代码:

-(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;
}

2 个答案:

答案 0 :(得分:6)

试试这个:

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

-(void)function2{

//some code..
}

并且,使用“gotos”只允许在非常非常特殊的情况下,你现在应该忘记它们!它再次成为任何发展原则。

答案 1 :(得分:2)

关于你的编辑,将start:逻辑放入它自己的方法中,然后从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];
}