2014-01-13 36 views
1

希望这是一个简单的。其他按钮标题上的按钮项目的基本IBAction项目

以下是处理:我有一个按钮项目,“选择”,打开一个选择器。当用户从选取器中选择一些东西时,“选择”按钮的标题变为选择并且选择器在视野外动画。现在我已经创建了第二个“搜索”按钮来查询Google Places API,但是我需要它不查询ITS标题,因为它现在正在执行,但是标题是OTHER按钮。合理?

这是相关的代码。我是积极的,这是一个简单的调整,但我是Objective-C的新手,所以我仍然围绕着事情的运作方式。

ViewController.m

#import "RendezvousViewController.h" 

@interface RendezvousViewController() 

@end 

@implementation RendezvousViewController 

// returns the number of 'columns' to display. 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

// returns the # of rows in each component.. 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return [self.userSelectArray count]; 
} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    return [self.userSelectArray objectAtIndex:row]; 
} 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 

    NSLog(@"Selected Row %ld", (long)row); 
    switch(row) 
    { 

     case 0: 
      self.userSelection.title = @"Breakfast"; 
      break; 
     case 1: 
      self.userSelection.title = @"Brunch"; 
      break; 
     case 2: 
      self.userSelection.title = @"Lunch"; 
      break; 
     case 3: 
      self.userSelection.title = @"Dinner"; 
      break; 
     case 4: 
      self.userSelection.title = @"Bar"; 
      break; 
     case 5: 
      self.userSelection.title = @"Late Night Snack"; 
      break; 
    } 

} 

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.userSelectArray = [[NSArray alloc]   initWithObjects:@"Breakfast",@"Brunch",@"Lunch",@"Dinner",@"Bar",@"Late Night Snack" , nil]; 

    //Make this controller the delegate for the map view. 
    self.mapView.delegate = self; 

    // Ensure that you can view your own location in the map view. 
    [self.mapView setShowsUserLocation:YES]; 

    //Instantiate a location object. 
    locationManager = [[CLLocationManager alloc] init]; 

    //Make this controller the delegate for the location manager. 
    [locationManager setDelegate:self]; 

    //Set some parameters for the location object. 
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
    firstLaunch=YES; 
} 

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

- (IBAction)toolbarButtonPress:(id)sender { 
    UIBarButtonItem *button = (UIBarButtonItem *)sender; 
    NSString *buttonTitle = [button.title lowercaseString]; 
    [self queryGooglePlaces:buttonTitle]; 
    //Use the above title text to build the URL query and get the data from Google. 
} 

ViewController.m

@interface RendezvousViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate, UIPickerViewDataSource,UIPickerViewDelegate> 

{ 
    CLLocationManager *locationManager; 
    CLLocationCoordinate2D currentCentre; 
    int currenDist; 
    BOOL firstLaunch; 
} 

@property (strong, nonatomic) IBOutlet MKMapView *mapView; 
@property (strong, nonatomic) IBOutlet UIBarButtonItem *userSelection; 
@property (strong, nonatomic) IBOutlet UIBarButtonItem *searchButton; 
@property (strong, nonatomic) IBOutlet UIPickerView *userPicker; 
@property (strong, nonatomic)   NSArray *userSelectArray; 

@end 

和往常一样,在此先感谢。

回答

1

如果您在视图上将按钮设为属性,您可以直接访问它(而不是使用发件人)。

但是为什么不直接从选取器中直接从按钮标题中获取它?

编辑:

所以根据你的代码,你要添加另一个按钮

@implementation RendezvousViewController{ 
    UIButton *selectButton; 
} 

Initalize它

-(void)viewDidLoad { 
    selectButton = [[UIButton alloc] init]; 
    ... 
} 

然后设置在选择器标题

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 

    NSLog(@"Selected Row %ld", (long)row); 
    switch(row) 
    { 
     case 0: 
      self.selectButton.title = @"Breakfast"; 
      break; 
     ... 
    } 
} 

然后抓住它当选择你的其他按钮:

- (IBAction)toolbarButtonPress:(id)sender { 
    NSString *buttonTitle = [self.selectButton.title lowercaseString]; 
    [self queryGooglePlaces:buttonTitle]; 
    //Use the above title text to build the URL query and get the data from Google. 
} 



@implementation RendezvousViewController{ 
    UIButton *selectButton; 
    NSString *selection; 
} 

不过我建议,有另一个属性只是存储选择,而不是依赖于你的用户界面,如按钮标题值。它应该是这样的:

Initalize它

-(void)viewDidLoad { 
    selectButton = [[UIButton alloc] init]; 
    selection = @""; 
    ... 
} 

然后设置标题的选择器

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 

    NSLog(@"Selected Row %ld", (long)row); 
    switch(row) 
    { 
     case 0: 
      selection = @"Breakfast"; 


      break; 
     ... 
    } 
    self.selectButton.title = selection; 
} 

然后在选择你的其他按钮抓住它:

- (IBAction)toolbarButtonPress:(id)sender { 
    [self queryGooglePlaces:selection]; 
    //Use the above title text to build the URL query and get the data from Google. 
} 
+0

一旦进行了选择,第一个按钮将直接从选取器中取得其标题(开始为“选择”,然后更改为用户选择的任何内容)。第二个按钮然后需要选择并搜索它。我这样做是为了让用户在选择器滑开时能够看到他们的选择,但现在我无法使用“搜索”按钮来搜索被选中的内容而不是自己的标题。 – jeremytripp

+0

我明白了,但发件人总是被按下的按钮,因此您需要从其他地方获取搜索字词。为什么不从搜索器中获取搜索词并更新按钮名称。但是,如果您以后由于某种原因决定更改按钮名称格式,则此方法仍然可以按预期工作,因为它不依赖于按钮名称。如果你张贴更多的代码,我可以尝试举一个我的意思。 – ansible

+0

啊,我认为这是有道理的。我刚刚编辑了问题以包含更多相关的代码。对不起,如果它超载。我只想包括与选取器和按钮相关的部分,但并未意识到这是多少。有趣的是,苹果如何让一些事情变得如此简单,其他事情如此复杂! – jeremytripp

0
if([buttonTitle isEqualToString:"Select"]) 
    return; //no searching 
else{ 
    [self queryGooglePlaces:buttonTitle]; 
} 
+0

谢谢,santhu,这是我现有的代码中的一个值得补充,以防止搜索,直到做出选择,但我需要的是第二个按钮来引用第一个按钮的标题或选择器选择。两者都可以工作,只要通过我现有的代码就可以更容易地引用第一个按钮的标题。 – jeremytripp

+0

@ansible你试图访问toolbarButtonPress方法中选择按钮的标题? – santhu