【问题标题】:Issues setting object properties with UISegmentedControl使用 UISegmentedControl 设置对象属性的问题
【发布时间】:2014-04-23 02:26:19
【问题描述】:

我想要做的是让两个 UISegmentedControl 框将 itemCondition 和 itemLocation 对象属性设置为各自的值,具体取决于按下的按钮。

然后,该值将作为参数发送到 Parse 云代码函数,如 submitButton 函数所示。我知道以 minPrice 和 maxPrice 为例,这些是用户提供的值,因此您可以将 self.minPrice.text 作为参数发送。

将 UISegmentedControl 设置为 self.itemCondition 并将其作为参数发送似乎不起作用。由于分段按钮是设置值的对象,而不是用户在文本框中明确键入内容,我认为它的完成方式不同,我只是不确定如何。

这是来自我的 criteriaViewController 的代码,用于处理提到的所有内容。

- (IBAction)conditionToggle:(id)sender{

    if(Segment.selectedSegmentIndex == 0){
        self.itemCondition == 'new';
    }
    else if(Segment.selectedSegmentIndex == 1){
        self.itemCondition == 'any';
    }

}

- (IBAction)itemLocationToggle:(id)sender {

    if(Segment.selectedSegmentIndex == 0){
        self.itemLocation == 'US';
    }
    else if(Segment.selectedSegmentIndex == 1){
        self.itemLocation == 'WorldWide';
    }
}

- (IBAction)submitButton:(id)sender
{
    if (self.itemSearch.text.length > 0) {

        //add all the info to users respective category object


        //perform search with criteria just submitted
        [PFCloud callFunctionInBackground:@"eBayCriteriaSearch"
                           withParameters:@{@"item": self.itemSearch.text,
                                            @"minPrice": self.minPrice.text,
                                            @"maxPrice": self.maxPrice.text,
                                            @"itemCondition": self.itemCondition,
                                            @"itemLocation": self.itemLocation,}
                                    block:^(NSString *result, NSError *error) {

                                        if (!error) {
                                            NSLog(@"The result is '%@'", result);

                                            if ([result intValue] == 1) {
                                                [self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self];
                                            } else {
                                                [self performSegueWithIdentifier:@"ShowCriteriaSegue" sender:self];
                                            }

                                        }
                                    }];
    }
}

【问题讨论】:

    标签: ios objective-c uisegmentedcontrol


    【解决方案1】:

    在你对 self.itemCondition 的赋值中有一个双等号。那是一个比较运算符。您只需要使用 '=' 来分配一个值。

    另外,如果 itemCondition 是 NSString,那么您需要更改每个赋值的语法,使其看起来像这样:

    self.itemCondition = @"new";

    注意@和双引号。

    另外,我可能会使用 switch 语句而不是其他几个 if 语句,因为将来您可能会有更多段。所以这就是你的 itemCondition 分段控件的事件方法如何寻找我。

    - (IBAction)mySegmentedControl_ValueChanged:(id)sender {
    
        switch (self.mySegmentedControl.selectedSegmentIndex) {
    
            default:
            case 0:
                self.itemCondition = @"new";
                break;
    
            case 1:
                self.itemCondition = @"any";
                break;
        }
    }
    

    【讨论】:

    • 我明白你的意思。在这种情况下,mySegmentedControl 是什么?这是我必须创建的属性吗?
    • 这是一个属性,通常是一个 IBOutlet。它看起来像这样:@property (weak, nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;您可以通过 Interface Builder developer.apple.com/library/ios/recipes/… 创建它
    • 如果是,请不要忘记将其标记为正确答案,或者如果它对您有所帮助,请点赞。
    猜你喜欢
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多