我对 Objective-c 和 Xcode 都很陌生。我正在尝试实现一个简单的 UIPickerView + 工具栏设置,当用户点击右侧的 Label 元素时显示,然后当用户从 PickerView 中选择一个选项时,Label 元素将被所选选项的值替换。
目前我在 UIVIew 元素中有一个 UIPickerView 和一个工具栏。我有 UIVIew 工作的上下滑动动画。我遇到的问题是动画的定位问题。请看截图:
当我单击 UIPickerView 上方工具栏上的“取消”或“完成”按钮时,它会正确隐藏整个 UIView,但是当我单击“显示”按钮以启动 UIView 时,它的重新定位不够高UIView 可见。有谁知道为什么会这样?
当我通过将动画 y 轴值设置为 144 而不是 244 来强制 UIView 重新定位更高时,它看起来像这样:
UIPickerView 的高度似乎在动画期间发生了变化。我将如何解决这个问题?
这是我的 .h 和 .m 文件中的代码:
。H
#import <UIKit/UIKit.h>
@interface NewInvoiceViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
NSArray *storedClients;
NSString *selectedClient;
}
- (IBAction)cancel:(id)sender;
- (IBAction)clientSelectCancel:(id)sender;
- (IBAction)clientSelectDone:(id)sender;
- (IBAction)clientPickerShowBtn:(id)sender;
@property (strong, nonatomic) IBOutlet UIPickerView *clientPicker;
@property (strong, nonatomic) IBOutlet UILabel *clientLabel;
@property (strong, nonatomic) IBOutlet UIView *clientPickerViewContainer;
@end
.m
#import "NewInvoiceViewController.h"
@interface NewInvoiceViewController ()
@end
@implementation NewInvoiceViewController
@synthesize clientLabel, clientPicker, clientPickerViewContainer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
storedClients = [NSArray arrayWithObjects:@"Joe Smith",@"John Doe",@"Bob Johnson",@"Joel Smith", nil];
clientPickerViewContainer.frame = CGRectMake(0, 522, 320, 261);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cancel:(id)sender{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)clientSelectCancel:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
clientPickerViewContainer.frame = CGRectMake(0, 520, 320, 260);
[UIView commitAnimations];
}
- (IBAction)clientSelectDone:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
clientPickerViewContainer.frame = CGRectMake(0, 520, 320, 260);
[UIView commitAnimations];
}
- (IBAction)clientPickerShowBtn:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
clientPickerViewContainer.frame = CGRectMake(0, 244, 320, 260);
[UIView commitAnimations];
}
// 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 [storedClients count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [storedClients objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
selectedClient = [storedClients objectAtIndex:row];
}
@end
Xcode 故事板的屏幕截图:http: //i.imgur.com/j8q6T9N.png 我选择了 UIView 以便位置和尺寸值可见。
使用的教程:http: //i-software-developers.com/2012/05/27/xcode-4-2-show-and-hide-pickerview/
这是实现这样的设置的最佳方式吗?我已经看到使用称为 actionSheet 的其他示例,使用它会更好吗?
actionSheet 示例:http ://pastebin.com/5MC9JJC0
非常感谢所有帮助或建议!
谢谢