如果语句在xcode中以编程方式创建标签时不起作用

时间:2012-06-03 05:09:58

标签: objective-c xcode if-statement uilabel

我在一个方法中设置了一个if语句来设置选项卡视图控制器中的问题(标签)。代码从xml文件和当前选项卡视图标题中获取数据以设置问题。在调试代码运行的第一次时,它给我“Page 1”==“Page 1”并执行操作,但当我在选项卡栏上选择另一个视图控制器时,我得到“Page 2”==“Page 2”虽然调试,但它没有使用if语句执行逻辑。如果我做(1){逻辑}它同时执行逻辑,但不将我的问题分成第1页和第2页。视图和视图名称是使用同一个xml文件中的信息动态创建的,所以没有错误拼写的机会。

以下是相关代码。任何帮助都会很好,因为我经过几个小时的尝试思考并在网上搜索,试图弄清楚为什么一个真正的if语句在第二次通过代码时没有工作时头疼。

//setup questions at runtime  --not working--

-(void)setupQuestions{

    int lableY = 85;

    NSString *pageTital = self.pageTabViewLable.title;
    NSString *questionPage;


    for (int i = 0; i < self.question_array.count; i++) {

        self.currentQuestion = [self.question_array objectAtIndex:i];  //Get information of current Question from array

        questionPage = self.currentQuestion.Page;

        if (pageTital == questionPage){

            //Create a Dynamic Label for Question.
            UILabel *lablel;
            CGRect lableFrame = {155,lableY,600,25};
            lablel = [[UILabel alloc] initWithFrame:lableFrame];
            lablel.text = self.currentQuestion.Question;
            [self.view addSubview:lablel];
            lableY += 90;               //lable spacing

        }
    }
} 




//at runtime setup tabs  --working--

-(void)setupTabPages{



    NSMutableArray* newArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];  //Create a array to hold tab veiws

    if( newArray.count != self.totalTabPageCount)  //check to see is amount of veiws and cussrent reated views are the same
    {

       self.pageTabViewLable.title = [xmlParser1 getPageWithIndexLocation:0];  //set text of inital view

        UIStoryboard *storyboard = self.storyboard; //get storyboard context information from view name

        if (self.totalTabPageCount != -1) {     //check for xml error

            for (int i = 1; i < self.totalTabPageCount; i++) {  //create additional views from storyboard view
                Check_StratusViewController *cvc = [storyboard instantiateViewControllerWithIdentifier:@"Check_StratusViewController1"];
                cvc.pageTabViewLable.title = [xmlParser1 getPageWithIndexLocation:i];
                [newArray addObject:cvc];


            }

            [self.tabBarController setViewControllers:newArray animated:YES];  //add the views
        } 

    }

    self.totalQuestionCount = [xmlParser1 getTotalQuestionCount];

    self.currentQuestion = [[Question alloc] init];
    self.question_array = [[NSMutableArray alloc] init];

    for (int i = 0; i < self.totalQuestionCount; i++) {
        [self.question_array addObject:[xmlParser1 getQuestionAtIndelLocation:i]];
    }


    [self setupQuestions];


}

1 个答案:

答案 0 :(得分:2)

你不应该使用pageTital == questionPage来比较2个字符串,你应该使用[pageTital isEqualToString questionPage]