希望這是一個簡單的。其他按鈕標題上的按鈕項目的基本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
和往常一樣,在此先感謝。
一旦進行了選擇,第一個按鈕將直接從選取器中取得其標題(開始爲「選擇」,然後更改爲用戶選擇的任何內容)。第二個按鈕然後需要選擇並搜索它。我這樣做是爲了讓用戶在選擇器滑開時能夠看到他們的選擇,但現在我無法使用「搜索」按鈕來搜索被選中的內容而不是自己的標題。 – jeremytripp
我明白了,但發件人總是被按下的按鈕,因此您需要從其他地方獲取搜索字詞。爲什麼不從搜索器中獲取搜索詞並更新按鈕名稱。但是,如果您以後由於某種原因決定更改按鈕名稱格式,則此方法仍然可以按預期工作,因爲它不依賴於按鈕名稱。如果你張貼更多的代碼,我可以嘗試舉一個我的意思。 – ansible
啊,我認爲這是有道理的。我剛剛編輯了問題以包含更多相關的代碼。對不起,如果它超載。我只想包括與選取器和按鈕相關的部分,但並未意識到這是多少。有趣的是,蘋果如何讓一些事情變得如此簡單,其他事情如此複雜! – jeremytripp