简体   繁体   English

UITextView和UIPickerView都有自己的UIToolbar

[英]UITextView and UIPickerView with its own UIToolbar

I like to replicate the form behavior of Safari on the iPhone in my own app. 我想在我自己的应用程序中复制Safari在iPhone上的表单行为。 If you enter data in an web form you get a separate UIToolbar (previous, next, done) just above the UIKeyboardView. 如果您在Web表单中输入数据,您将在UIKeyboardView上方获得单独的UIToolbar(上一个,下一个,完成)。 Same for choosing an option: you get the same UIToolbar just above an UIPickerView. 选择一个选项也是一样的:你在UIPickerView上面得到了相同的UIToolbar。

I am looking for demos / sourcode / ideas how to implement this. 我正在寻找演示/ sourcode / ideas如何实现这一点。 Would I create my own subview with that toolbar and textview / pickerview? 我会使用该工具栏和textview / pickerview创建自己的子视图吗? Is there a more elegant way? 有更优雅的方式吗? Especially something that leverages becomeFirstResponder of UITextfield? 尤其是利用UITextfield成为第一个响应者的东西?

So i created a UIViewCOntroller subclass to manage this. 所以我创建了一个UIViewCOntroller子类来管理它。

on that i wrote this function to add. 在那我写这个功能添加。

-(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];
 }

I then created the view in IB, here is what my class Header looked like at the end of that. 然后我在IB中创建了视图,这是我的类Header在结束时的样子。 (there is also a UItoolbar on the view i just do not have a reference to it in my Controller) (视图上还有一个UI工具栏,我在Controller中没有对它的引用)

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

To then hide the view i use. 然后隐藏我使用的视图。

-(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];
}

And last but not least all the delegate functions for the picker. 最后但并非最不重要的是选择器的所有委托函数。

    - (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];
        }
    }

This is how you do the ToolBar. 这就是你如何使用ToolBar。 Basically i use the same concept with a ViewController subclass, and i dont use the standard push view or modal display options. 基本上我使用与ViewController子类相同的概念,我不使用标准推视图或模态显示选项。 (the example here actually places a Textbox and a toolbar on top of the keyboard. (这里的示例实际上将文本框和工具栏放在键盘顶部。

@interface BugEditCommentController : UIViewController { UITextView* Comment; @interface BugEditCommentController:UIViewController {UITextView * Comment; UIToolbar* Toolbar; UIToolbar *工具栏; } -(void) addToViewWithAnimation:(UIView*) theView; } - (void)addToViewWithAnimation:(UIView *)theView;

To activate this view usually you would call [object becomeFirstResponder]; 要激活此视图,通常会调用[object becomeFirstResponder]; so if you add this to your view Controller constructor, all you need to do is call [object becomeFirstResponder]; 因此,如果将其添加到视图Controller构造函数中,您需要做的就是调用[object 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 if you implement this method on your controller (defined in the above code) 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];
}

hope the additional info helps. 希望其他信息有所帮助。

I'm looking for the solution for this issue too. 我也在寻找这个问题的解决方案。

I found this was the best solution, you can use this SCKit to add tool bar to dismiss the UIPickerView or the UIDatePicker as you want. 我发现这是最好的解决方案,您可以使用此SCKit添加工具栏以根据需要关闭UIPickerView或UIDatePicker。

Following is github link: https://github.com/scelis/SCKit/tree/ 以下是github链接: https//github.com/scelis/SCKit/tree/

Have fun! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM