2012-05-06 75 views
0

所以,我跟着一個關於如何獲得一個UIDatePicker而不是鍵盤的教程,當用戶點擊一個UITextField時。我得到的問題是,當選擇器啓動後,會短促的,它給在控制檯此消息:showInView正在造成剪輯

Presenting action sheet clipped by its superview. Some controls might not respond to touches. On iPhone try -[UIActionSheet showFromTabBar:] or -[UIActionSheet showFromToolbar:] instead of -[UIActionSheet showInView:]. 

我試過其他方法,但是他們沒有任何工作,也沒有我希望他們至。如果任何人都可以看看下面的代碼並提供一些建議,那就太好了。謝謝:)

AddShiftViewController.h

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 
#import "Shift.h" 

@interface AddShiftsViewController : UIViewController <UITextFieldDelegate> 

@property (weak, nonatomic) IBOutlet UITextField *jobTitleField; 
@property (weak, nonatomic) IBOutlet UITextField *startDateField; 
@property (weak, nonatomic) IBOutlet UITextField *endDateField; 

@property (nonatomic, strong) NSDate *startDate; 
@property (nonatomic, strong) NSDate *endDate; 

@property (nonatomic, strong) UIActionSheet *dateSheet; 

- (void)setDate; 
- (void)dismissPicker; 
- (void)cancelPicker; 

- (IBAction)addShift:(id)sender; 

@end 

AddShiftViewController.m

#import "AddShiftsViewController.h" 

@interface AddShiftsViewController() 

@end 

@implementation AddShiftsViewController 
@synthesize jobTitleField; 
@synthesize startDateField; 
@synthesize endDateField; 
@synthesize startDate, endDate, dateSheet; 

- (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. 

    AppDelegate *dataCenter = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
} 

- (void)viewDidUnload 
{ 
    [self setJobTitleField:nil]; 
    [self setStartDateField:nil]; 
    [self setEndDateField:nil]; 
    [self setEndDate:nil]; 
    [self setStartDate:nil]; 
    [self setDateSheet:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (IBAction)addShift:(id)sender { 
    Shift *newShift = [[Shift alloc] init]; 
} 

- (void)setDate { 
    dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 

    [dateSheet setActionSheetStyle:UIActionSheetStyleDefault]; 

    CGRect pickerFrame = CGRectMake(0, 44, 0, 0); 
    UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:pickerFrame]; 
    [picker setDatePickerMode:UIDatePickerModeDateAndTime]; 

    [dateSheet addSubview:picker]; 

    UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)]; 

    [controlToolBar setBarStyle:UIBarStyleDefault]; 
    [controlToolBar sizeToFit]; 

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

    UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"Set" style:UIBarButtonItemStyleDone target:self action:@selector(dismissPicker)]; 

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelPicker)]; 

    [controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO]; 

    [dateSheet addSubview:controlToolBar]; 

    [dateSheet showInView:self.view]; 

    [dateSheet setBounds:CGRectMake(0, 0, 420, 385)]; 
} 

- (void)dismissPicker { 
    NSArray *listOfViews = [dateSheet subviews]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MMMM d, yyyy at H:mm"]; 

    for (UIView *subView in listOfViews) { 
     if ([subView isKindOfClass:[UIDatePicker class]]) { 
      if (self.startDateField.isEditing) { 
       self.startDate = [(UIDatePicker *)subView date]; 
       [self.startDateField setText:[formatter stringFromDate:self.startDate]]; 
       [dateSheet dismissWithClickedButtonIndex:0 animated:YES]; 
      } 
      else if (self.endDateField.isEditing) { 
       self.endDate = [(UIDatePicker *)subView date]; 
       [self.endDateField setText:[formatter stringFromDate:self.endDate]]; 
       [dateSheet dismissWithClickedButtonIndex:0 animated:YES]; 
      } 
     } 
    } 
} 

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
    [self setDate]; 
    return NO; 
} 

- (void)cancelPicker { 
    [dateSheet dismissWithClickedButtonIndex:0 animated:YES]; 
} 

@end 

Shifts.h(定製類)

#import <Foundation/Foundation.h> 

@interface Shift : NSObject 

@property (nonatomic, strong) NSString *jobTitle; 
@property (nonatomic, strong) NSString *employer; 
@property (nonatomic, strong) NSDate *startDate; 
@property (nonatomic, strong) NSDate *endDate; 

@end 

編輯

所以,我想切換到:

[dateSheet showFromTabBar:self.tabBarController.tabBar]; 

但它仍然是剪裁。我附上了一張圖片,以向您展示到底發生了什麼。

enter image description here

回答

3

看起來你可能得到的數字在這條線有點過:

[dateSheet setBounds:CGRectMake(0, 0, 420, 385)]; 

應該是:

[dateSheet setBounds:CGRectMake(0, 0, 320, 485)]; 

w iPhone屏幕的寬度不是420像素:)經典屏幕在Retina屏幕上寬320(縱向)或寬640。雖然在其他地方也可能有錯誤,但這一點對我來說很重要。

+0

是的,就是這樣:)謝謝Nethfel。 – Barks

0

首先,你確定邊框的寬度N高度是正確的。

CGRect pickerFrame = CGRectMake(0, 44, 0, 0); // (params) x , y, width, height 

使用此框架的選取器視圖具有0寬度和0高度,因此它將無法正確顯示其自身。嘗試使用某種寬度或高度創建UIDatePicker。

另外,你是否試圖顯示UIActionSheet從屬於UITabBarController的控制器...? 如果是這樣,那麼代替

[datesheet showInView:self.view]; 

使用

[datesheet showFromTabBar:self.tabBarController.tabBar]; 
+0

這些是在教程中使用的值,他們工作正常,所以我不認爲就是這樣。無論如何,我嘗試將它們分別設置爲320和300,但它沒有改變任何東西。我附上了一張顯示發生了什麼的截圖。我試圖鏈接它使用showFromTabBar但沒有運氣:( – Barks