通过完成按钮以编程方式关闭ModalView

时间:2012-01-13 16:22:57

标签: ios iphone

我必须点击“完成”按钮以编程方式解除模态视图。我认为UIButton优于UIBarButtonItem来添加UIControlEventsTouchupInside

但是UIButton我很困惑应该使用哪种按钮类型。

   UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];

[button addTarget:self action:@selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];    

  UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                          target:self
                                                                          action:@selector(dismissViewaction:)] autorelease];

2 个答案:

答案 0 :(得分:1)

您很可能想要使用UIButtonTypeRoundedRectUIButtonTypeCustom

使用自定义类型,您可以添加图像以供显示 您可以尝试从UIBarButtonItemUIBarItem中定义的图片属性)中“窃取”这些图片,并使自定义按钮看起来像UIBarButtonSystemItemDone按钮

UIBarButtonItem * buttonForImage = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil                                                                       action:nil] autorelease];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button addTarget:self action:@selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];

[button setImage:buttonForImage.image forState:UIControlStateNormal];

需要注意的事项。为UIButton设置图像时,根据contentMode属性,它不会缩放到按钮的大小。如果您希望图片遵循contentMode属性的规则,请改用setBackgroundImage: forState:

答案 1 :(得分:0)

我认为按钮的类型并不重要。您必须为按钮指定操作或选择器。 像这样:

UIBarButtonItem *bttItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(yourBttAction:)] autorelease];

行动:

- (IBAction) yourBttAction:(id)sender
{
    NSLog(@"Done Button clicked");

    //do something
}

如果按钮在modalViewController上,我通常,为了解雇它,我使用:

[self dismissModalViewControllerAnimated:(BOOL)];

//if you have a navigationController
[self.navigationController dismissModalViewControllerAnimated:(BOOL)];

但如果您想使用委托来解雇它,请查看此tutorial