希望这是一个简单的。其他按钮标题上的按钮项目的基本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