2009-05-19 73 views
2

我喜歡在我自己的應用程序中複製iPhone上的Safari的表單行爲。如果您在Web表單中輸入數據,則會在UIKeyboardView上方獲得單獨的UIToolbar(上一個,下一個,完成)。選擇一個選項也是一樣的:在UIPickerView上方獲得相同的UIToolbar。UITextView和UIPickerView有自己的UIToolbar

我正在尋找演示/ sourcode /想法如何實現這一點。我會用該工具欄和textview/pickerview創建我自己的子視圖嗎?有沒有更優雅的方式?特別是利用UITextfield的FirstResponder的東西?

回答

13

所以我創建了一個UIViewCOntroller子類來管理這個。

對此,我寫了這個函數來添加。

-(void) addToViewWithAnimation:(UIView *) theView 
{ 
    UIView* myview = self.view; 
    CGRect frame = myview.frame; 
    frame.origin.y = 420; 
    myview.frame = frame; 

    UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; 
    bgView.backgroundColor = [UIColor blackColor]; 
    bgView.alpha = 0.6; 
    backgroundView = bgView; 
    [theView addSubview: bgView]; // this adds in the dark background 

    [theView addSubview:self.view]; // this adds in the pickerView with toolbar. 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    frame = myview.frame; 
    frame.origin.y = 420 - frame.size.height; 
    myview.frame = frame; 

    [UIView commitAnimations]; 
} 

然後我在IB中創建了視圖,這裏是我的類標題看起來像在結束時的樣子。 (也有一個UItoolbar的視圖,我只是沒有在我的控制器中引用它)

@interface PropertyPickerController : UIViewController { 
    IBOutlet UIPickerView* Picker; 
    IBOutlet UIButton* DoneButton; 
    IBOutlet UIButton* CancelButton; 
    UIView* backgroundView; 
    NSArray* SimpleObjects; 
    id PickerObjectDelegate; 
    SEL PickerObjectSelector; 
} 

然後隱藏我使用的視圖。

-(void) removeFromSuperviewWithAnimation 
{ 

    UIView* myview = self.view; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(AnimationDidStop:)]; 
    [UIView setAnimationDuration:0.5]; 
    // set fram below window. 
    CGRect frame = myview.frame; 
    frame.origin.y = 420; 
    myview.frame = frame; 
    backgroundView.alpha = 0; //fades shade to nothing 
    [UIView commitAnimations]; 
} 

-(void) AnimationDidStop:(id) object 
{ 
    [self.view removeFromSuperview]; //removes view after animations. 
    [backgroundView removeFromSuperview]; 
} 

最後但並非最不重要的所有代表功能的選擇器。

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
    { 
     FBSimpleObject* object = (FBSimpleObject*)[SimpleObjects objectAtIndex:row]; 
     return object.Name; 
    } 

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
    { 
    } 
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
    { return 1;} 

    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
    { 
     return [SimpleObjects count]; 
    } 

    - (IBAction)CancelButtonClick 
    { 
      [self removeFromSuperviewWithAnimation]; 
    } 
    - (IBAction)DoneButtonClick 
    { 
//This performs a selector when the done button is clicked, makes the controller more versatile. 
if(PickerObjectDelegate && PickerObjectSelector) 
     { 
      NSMethodSignature* signature = [PickerObjectDelegate methodSignatureForSelector:PickerObjectSelector]; 
      NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 
      [invocation setTarget:PickerObjectDelegate]; 
      [invocation setSelector:PickerObjectSelector]; 
      [invocation setArgument:&object atIndex:2]; 
      [invocation retainArguments]; 
      [invocation invoke]; 
     } 
    } 

這就是你如何做ToolBar。基本上我使用與ViewController子類相同的概念,我不使用標準的推視圖或模式顯示選項。 (這裏的例子實際上放置一個文本框,並在鍵盤的頂部的工具欄

@interface BugEditCommentController:{的UIViewController 的UITextView *評論; UIToolbar *工具欄; } - (無效)addToViewWithAnimation:(UIView的*) theView;

來激活此視圖通常你會打電話[對象becomeFirstResponder]; 因此,如果您添加到您的視圖控制器的構造函數,所有你需要做的就是調用[對象becomeFirstResponder];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
    [nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil]; 
    [nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil]; 

ABD如果您實現您的控制器上這種方法(在上面的代碼中定義)

-(void) keyboardWillShow:(NSNotification *) note 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 

    CGRect toolbarFrame = Toolbar.frame; 
    CGRect keyboardFrame; 
    CGPoint keyboardCenter; 
    [[note.userInfo valueForKey:UIKeyboardCenterEndUserInfoKey] getValue:&keyboardCenter]; 
    [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardFrame]; 

    //CGRect toolbarRect = Toolbar.center; 
    toolbarFrame.origin.y= keyboardCenter.y - ((keyboardFrame.size.height/2) + (toolbarFrame.size.height)); 
    Toolbar.frame = toolbarFrame; 
    [UIView commitAnimations]; 

} 

-(void) keyboardWillHide:(id) object 
{ 

//you could call [self removeFromSuperviewHere]; 
} 

-(void) removeFromsuperViewWithAnimation 
{ 
    [Comment resignFirstResponder]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(AnimationDidStop:)]; 

    CGRect frame = Toolbar.frame; 
    frame.origin.y = 480; 
    Toolbar.frame = frame; 

    [self.view viewWithTag:1].alpha = 0; //fade transparent black background to clear. 
    [UIView commitAnimations]; 

} 
-(void)AnimationDidStop:(id) object 
{ 
    [self.view removeFromSuperview]; 
} 

希望更多的信息幫助。

+0

謝謝。但是UITextView丟失了。我不知道如何將其與'firstResponder'機制整合。 – luebken 2009-05-20 07:52:38

0

我正在尋找這個問題的解決方案。

我發現這是最好的解決方案,你可以使用這個SCKit來添加工具欄來關閉UIPickerView或者UIDatePicker。

以下是GitHub的鏈接:https://github.com/scelis/SCKit/tree/

玩得開心!