希望这很简单。
这是交易:我有一个按钮项目,“选择”,打开一个选择器。当用户从选择器中选择某些内容时,“选择”按钮的标题将更改为选择,并且选择器将动画显示在视图之外。现在我已经创建了第二个“搜索”按钮,它将查询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
一如既往,提前谢谢。
答案 0 :(得分:1)
如果您将按钮设为视图中的属性,则可以直接访问该按钮(而不是使用发件人)。
但是为什么不直接从选择器中获取它而不是间接地从按钮标题中获取它?
编辑:
因此,根据您的代码,您需要添加另一个按钮
@implementation RendezvousViewController{
UIButton *selectButton;
}
初始化
-(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;
}
我建议让另一个属性只存储所选的值,而不是依赖于你的UI,比如按钮标题。它看起来像这样:
初始化
-(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.
}
答案 1 :(得分:0)
if([buttonTitle isEqualToString:"Select"])
return; //no searching
else{
[self queryGooglePlaces:buttonTitle];
}