【问题标题】:Close UIViewController on button click单击按钮时关闭 UIViewController
【发布时间】:2012-10-29 08:09:14
【问题描述】:

我正在打开一个视图控制器(带有一个导航栏和一个用于关闭视图控制器的栏按钮项)。这是一个视图控制器,用于在用户单击“帮助”按钮时向用户显示信息。导航就像这个:ViewController1 -> “帮助”按钮 -> Viewcontroller2。 Viewcontroller2 有一个“关闭”按钮,可以关闭这个 ViewController。我有一个带有 IBAction 的关闭按钮连接到文件的所有者。但是当我单击“关闭”按钮时没有命中断点。代码是:

DetailViewController.m(viewcontroller1):

-(void) showHelp:(id)sender
{
    HelpViewController *helpWindow = [[HelpViewController alloc]initWithNibName:@"HelpViewController" bundle:[NSBundle mainBundle]];
    helpWindow.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentModalViewController:helpWindow animated:YES];
}

HelpViewController.m(viewcontroller2):

#import "HelpViewController.h"

@interface HelpViewController ()

@end

@implementation HelpViewController

@synthesize scrollView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)closeAction:(id)sender {
    [self dismissModalViewControllerAnimated:YES];

}

@end

我尝试在“closeAction”处设置断点,但没有命中。我是 iOS 开发的新手。我在这里遗漏了一些非常明显的东西。感谢任何帮助!

【问题讨论】:

    标签: uiviewcontroller xcode4.5 ibaction


    【解决方案1】:

    单击您的Close 按钮并在右侧工具中选择connection inspector 选项卡。在 touchUpInside 模式下检查您的按钮是否与closeAction: 此方法连接。

    如果没有,则将您的UIButton 插座连接到此方法-(IBAction)closeAction:(id)sender;,然后尝试。

    更新:如果您的按钮是UIBarButtonItem,那么将有一个选项为"New referencing outlet"。从中拖动并连接到您的方法。

    【讨论】:

    • “关闭”按钮是导航栏中的“栏按钮项”。当我单击“关闭”按钮的连接检查器时,没有“touchUpInside”选项。我在 .h 中有一个 IBOutlet,在同一个 .h 文件中也有一个 IBAction。如何将它们相互连接?
    • 我很抱歉听起来像个笨蛋!当我点击“新引用出口”并拖动到“关闭”按钮时,方法名称不会出现。我应该以不同的方式执行此操作吗?
    • 将关闭按钮的“新引用出口”拖到 viewController2 的文件所有者,它会显示一个选择 closeAction: 方法的选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多