UISegmentedControl未正确设置值

时间:2014-07-07 20:35:03

标签: ios objective-c uisegmentedcontrol

我的故事板中有两个UISegmentedControlconditionSegmentlocationSegment。从我的代码中可以看出,我希望将self.itemConditionself.itemLocation设置为各自的值,具体取决于用户点击的selectedSegmentIndex

当我在submitButton IBAction中对这些值进行NSLog时,它会将值显示为null。我没有正确地将按钮连接到IBAction吗?如果是这样,我该如何正确设置呢?

CriteriaViewController.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "SearchViewController.h"

@interface CriteriaViewController : UIViewController

//@property (nonatomic) IBOutlet UITextField *itemSearch;

@property (strong, nonatomic) IBOutlet UITextField *minPrice;
@property (strong, nonatomic) IBOutlet UITextField *maxPrice;
@property (strong, nonatomic) IBOutlet UISegmentedControl *conditionSegment;
@property (strong, nonatomic) IBOutlet UISegmentedControl *locationSegment;


@property (nonatomic, copy) NSNumber *chosenCategory;
@property (nonatomic, copy) NSString *chosenCategoryName;
@property (weak, nonatomic) NSString *itemCondition;
@property (weak, nonatomic) NSString *itemLocation;
@property (weak, nonatomic) NSString *itemSearch;

@end

CriteriaViewController.m:

#import "CriteriaViewController.h"

@interface CriteriaViewController ()
@property (weak, nonatomic) IBOutlet UISegmentedControl *itemConditionSegment;
@property (weak, nonatomic) IBOutlet UISegmentedControl *itemLocationSegment;


@end

@implementation CriteriaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    [super viewDidLoad];

    // Submit button
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // Create Round Rect Type button.
    submitButton.frame = CGRectMake(100, 100, 100, 100); // define position and width and height for the button.
    [submitButton setTitle:@"Submit" forState:UIControlStateNormal];

    [submitButton addTarget:self action:@selector(submitButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitButton];



}




- (IBAction) conditionSegment: (id)sender
{
    if(_conditionSegment.selectedSegmentIndex == 0)
    {
        // set condition to new
        self.itemCondition = @"New";
    }
    else if (_conditionSegment.selectedSegmentIndex == 1)
    {
        // set condition to all
        self.itemCondition = @"all";
    }
}

- (IBAction) locationSegment: (id)sender
{
    if(_locationSegment.selectedSegmentIndex == 0)
    {
        // set location to us
        self.itemLocation = @"US";
    }
    else if (_locationSegment.selectedSegmentIndex == 1)
    {
        // set clocation to worldwide
        self.itemLocation = @"Worldwide";
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






//add all the info to users respective new category object
- (IBAction)submitButton:(id)sender
{

    NSLog(@"'%@'", self.minPrice.text);
    NSLog(@"'%@'", self.maxPrice.text);
    NSLog(@"'%@'", self.itemCondition);
    NSLog(@"'%@'", self.itemLocation);

//    if (self.minPrice.text.length > 0 && self.maxPrice.text.length > 0) {
//    
//        [PFCloud callFunctionInBackground:@"userCategorySave"
//                           withParameters:@{@"categoryId": self.chosenCategory,
//                                            
//                                              @"minPrice": self.minPrice.text,
//                                            @"maxPrice": self.maxPrice.text,
//                                       @"itemCondition": self.itemCondition,
//                                        @"itemLocation": self.itemLocation,
//                                            
//                                        @"categoryName": self.chosenCategoryName,
//                                            }
//                                         block:^(NSString *result, NSError *error) {
//         
//                                             if (!error) {
//                                                 NSLog(@"Criteria successfully saved.");
//    
//                                                     [self performSegueWithIdentifier:@"SearchCategoryChooserToMatchCenterSegue" sender:self];
//
//                                             }
//                                         }];
//    
//    }

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

}


@end

0 个答案:

没有答案