2

私は自分のアプリで iPhone の Safari のフォーム動作を再現したいと思っています。Web フォームにデータを入力すると、UIKeyboardView のすぐ上に個別の UIToolbar (前、次、完了) が表示されます。オプションを選択する場合も同様です。UIPickerView のすぐ上に同じ UIToolbar が表示されます。

これを実装する方法のデモ/ソースコード/アイデアを探しています。そのツールバーとテキストビュー/ピッカービューを使用して独自のサブビューを作成しますか? もっとエレガントな方法はありますか?特に UITextfield の becomeFirstResponder を活用したものは?

4

2 に答える 2

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

これがツールバーのやり方です。基本的に、私はViewControllerサブクラスで同じ概念を使用し、標準のプッシュビューまたはモーダル表示オプションを使用しません。(ここでの例では、実際にはキーボードの上にテキストボックスとツールバーを配置しています。

@interface BugEditCommentController:UIViewController{UITextView*コメント; UIToolbar*ツールバー; }-(void)addToViewWithAnimation:(UIView *)theView;

このビューをアクティブにするには、通常、[objectbecomeFirstResponder]を呼び出します。したがって、これをView Controllerコンストラクターに追加する場合、必要なのは[objectbecomeFirstResponder]を呼び出すことだけです。

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

追加情報がお役に立てば幸いです。

于 2009-05-19T22:30:32.000 に答える
0

私もこの問題の解決策を探しています。

これが最善の解決策であることがわかりました。このSCKitを使用してツールバーを追加し、必要に応じてUIPickerViewまたはUIDatePickerを閉じることができます。

以下はgithubリンクです:https ://github.com/scelis/SCKit/tree/

楽しむ!

于 2012-02-18T12:18:53.233 に答える