如何将UIView作为动作表?

时间:2014-03-26 06:54:59

标签: ios objective-c uiview subview

我需要弹出一个视图并让用户登录。我显然不能使用动作表,因为它们只有按钮。

所以我想让UIView向上滑动。

我创建了UIView,名为CustomModalView。

我似乎需要这样的代码:

- (IBAction)showTapped:(id)sender {
    [UIView animateWithDuration:.5 animations:^{
    subView.frame=CGRectMake(0, 225, subView.frame.size.width, subView.frame.size.height);
    }];
    }

- (IBAction)hideTapped:(id)sender {
    [UIView animateWithDuration:.5 animations:^{
    subView.frame=CGRectMake(0, 480, subView.frame.size.width, subView.frame.size.height);
    }];
    }

但我是目标c的新手,我不明白如何根据我自己的项目自定义...我甚至不知道在哪里输入这些代码。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

您可以使用操作表执行此操作。 首先在界面声明uiactionsheet的属性。在下面的例子中,我声明了一个名为countryActionSheet的属性。

之后在IBaction中遵循代码;

-(IBAction)popupView{//  declare another actionSheet //
UIActionSheet *actionSheet =[[UIActionSheet alloc]initWithTitle:@"Select Country"       delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];

countryActionsheet=actionSheet;//equate both the actionSheets//

actionSheet.actionSheetStyle= UIActionSheetStyleBlackTranslucent;
countryPicker.dataSource=self;
countryPicker.delegate=self; 

// Add your view with frame with respect to actionSheet//

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
imageView.image=[UIImage imageNamed:@"black background.png"];

UIButton *cancelBtn=[[UIButton alloc]initWithFrame:CGRectMake(200,20, 80, 30)];
[cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(cancelBtnPressed) forControlEvents:UIControlEventTouchUpInside];// create a method named cancelBtnPressed
[cancelBtn setBackgroundImage:[UIImage imageNamed:@"Untitled 4.png"] forState:UIControlStateNormal];

UIButton *okBtn =[[UIButton alloc]initWithFrame:CGRectMake(40, 20, 80, 30)];
[okBtn setTitle:@"Done" forState:UIControlStateNormal];
[okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[okBtn addTarget:self action:@selector(okBtnPressed) forControlEvents:UIControlEventTouchUpInside];// create method named okBtnPressed
[okBtn setBackgroundImage:[UIImage imageNamed:@"Untitled 4.png"] forState:UIControlStateNormal];


[countryActionsheet addSubview:imageView];
[countryActionsheet addSubview:cancelBtn];
[countryActionsheet addSubview:okBtn];

[countryActionsheet showInView:self.view];
[countryActionsheet setBounds:CGRectMake(0, 0, 320, 400)];// frame of actionSheet
}

-(void)cancelbtnPressed{
     // write action
}

-(void)okBtnPressed{
     // write action
}

希望这可以帮到你;

答案 1 :(得分:1)

简单方法是使用添加视图UIActionSheet

    [actionSheeet addSubview: subView];

答案 2 :(得分:1)

我假设你已经有了一个UIViewController,它添加了一个CustomModalView,就像子视图一样。我在上面重写了两个方法:

- (void)showLoginView{
    [UIView animateWithDuration:.5 animations:^{
    customModalView.frame=CGRectMake(0, 225, customModalView.frame.frame.size.width, customModalView.frame.frame.size.height);
    }];
}

- (void)hideLoginView{
    [UIView animateWithDuration:.5 animations:^{
    customModalView.frame.frame=CGRectMake(0, 480, customModalView.frame.frame.size.width, customModalView.frame.frame.size.height);
    }];
}

在UIViewController.m中,调用[self showLoginView]显示,否则

答案 3 :(得分:1)

如果您只需要一个简单的登录提示,其中包含用户名和密码文本输入,以及登录和取消按钮,则可以使用UIAlertView。只需将其UIAlertViewStyle属性设置为UIAlertViewStyleLoginAndPasswordInput即可。 如果您之前没有使用过这些内容,请阅读Apple UIAlertViewUIAlertViewDelegate的文档。