【问题标题】:UITextView and UIPickerView with its own UIToolbar带有自己的 UIToolbar 的 UITextView 和 UIPickerView
【发布时间】:2010-10-27 11:11:58
【问题描述】:

我喜欢在我自己的应用程序中复制 iPhone 上 Safari 的表单行为。如果您在 Web 表单中输入数据,您会在 UIKeyboardView 上方获得一个单独的 UIToolbar(上一个、下一个、完成)。选择选项也是如此:您在 UIPickerView 上方获得相同的 UIToolbar。

我正在寻找如何实现它的演示/源代码/想法。我会用那个工具栏和 textview / pickerview 创建我自己的子视图吗?有没有更优雅的方式?特别是利用 UITextfield 的 becomeFirstResponder 的东西?

【问题讨论】:

    标签: iphone cocoa-touch uitextfield uitextview uipickerview


    【解决方案1】:

    所以我创建了一个 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 中创建了视图,这是我的类 Header 在最后的样子。 (视图上还有一个 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];
            }
        }
    

    这就是你如何做工具栏。基本上我对 ViewController 子类使用相同的概念,并且我不使用标准的推送视图或模式显示选项。 (此处的示例实际上在键盘顶部放置了一个文本框和一个工具栏。

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

    要激活这个视图,通常你会调用 [object becomeFirstResponder]; 所以如果你把它添加到你的视图控制器构造函数中,你需要做的就是调用 [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 如果你在你的控制器上实现这个方法(在上面的代码中定义)

    -(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 不见了。我不确定如何将其与“firstResponder”机制集成。
    • 对不起,这个例子只是 UIPickerView。有机会我会发布 UITextView 的代码。
    【解决方案2】:

    我也在寻找这个问题的解决方案。

    我发现这是最好的解决方案,您可以使用此 SCKit 添加工具栏以根据需要关闭 UIPickerView 或 UIDatePicker。

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

    玩得开心!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多