UITextView和UIPickerView及其自己的UIToolbar [英] UITextView and UIPickerView with its own UIToolbar

查看:109
本文介绍了UITextView和UIPickerView及其自己的UIToolbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在我自己的应用程序中复制Safari上的Safari的表单行为。如果您以Web表单的形式输入数据,您将在UIKeyboardView上方得到一个单独的UIToolbar(上一个,下一个,完成)。选择一个选项也一样:你在UIPickerView上面得到相同的UIToolbar。

I like to replicate the form behavior of Safari on the iPhone in my own app. If you enter data in an web form you get a separate UIToolbar (previous, next, done) just above the UIKeyboardView. Same for choosing an option: you get the same UIToolbar just above an UIPickerView.

我正在寻找demos / sourcode / ideas如何实现这个。我将创建自己的子视图工具栏和textview / pickerview?有更优雅的方式吗?特别是那些利用UITextfield的becomeFirstResponder的东西?

I am looking for demos / sourcode / ideas how to implement this. Would I create my own subview with that toolbar and textview / pickerview? Is there a more elegant way? Especially something that leverages becomeFirstResponder of UITextfield?

推荐答案

所以我创建了一个UIViewCOntroller子类来管理这个。

So i created a UIViewCOntroller subclass to manage this.

我写了这个函数来添加。

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

然后我在IB中创建了视图,这里是我的类Header看起来像在结束时。 (在视图中还有一个UItoolbar我只是没有在我的控制器中的引用)

I then created the view in IB, here is what my class Header looked like at the end of that. (there is also a UItoolbar on the view i just do not have a reference to it in my 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];
        }
    }

这是ToolBar的工作方式。基本上我使用相同的概念与ViewController子类,我不使用标准的推视图或模态显示选项。 (此处的示例实际上是在键盘顶部放置一个Textbox和一个工具栏。

This is how you do the ToolBar. Basically i use the same concept with a ViewController subclass, and i dont use the standard push view or modal display options. (the example here actually places a Textbox and a toolbar on top of the keyboard.

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

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

要激活这个视图通常你会调用[object如果你添加到你的视图控制器构造函数,你所需要做的是调用[object becomeFirstResponder];

To activate this view usually you would call [object becomeFirstResponder]; so if you add this to your view Controller constructor, all you need to do is call [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)

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

希望其他信息有帮助。

这篇关于UITextView和UIPickerView及其自己的UIToolbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆