2014-01-13 61 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