gpt4 book ai didi

ios - 按钮项的基础 IBAction 基于其他按钮项的标题

转载 作者:行者123 更新时间:2023-11-29 12:55:56 25 4
gpt4 key购买 nike

希望这是一个简单的过程。

事情是这样的:我有一个按钮项“选择”,它会打开一个选择器。当用户从选取器中选择某些内容时,“选择”按钮的标题会更改为所选内容,并且选取器会以动画形式消失。现在我已经创建了第二个“搜索”按钮,它将查询 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

一如既往,提前致谢。

最佳答案

如果您将按钮设为 View 的属性,您可以直接访问它(而不是使用发送器)。

但为什么不直接从选择器中获取而不是间接从按钮标题中获取呢?

编辑:

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

@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.
}

关于ios - 按钮项的基础 IBAction 基于其他按钮项的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21099463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com