xcode 如何在 iOS 故事板中添加按钮触摸操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22423170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:46:27  来源:igfitidea点击:

How to add an action on button touch up in iOS storyboard

iosxcodexcode-storyboard

提问by user220201

I am new to iOS app development. I am going through the dev guides. My app has two views - one with a button and clicking that button launches a new view. I added a segue to launch the new view and was able to link it to the button. But its not clear to me what the exact action that launches the new view and how I can handle that action. I want to do some network operation before launching the new view and so want to handle that button click event (touch up). Can I get Xcode to generate a dummy handler function for that specific event?

我是 iOS 应用程序开发的新手。我正在阅读开发指南。我的应用程序有两个视图 - 一个带有按钮,单击该按钮会启动一个新视图。我添加了一个 segue 来启动新视图,并且能够将它链接到按钮。但我不清楚启动新视图的确切操作是什么以及我如何处理该操作。我想在启动新视图之前做一些网络操作,所以想处理那个按钮点击事件(touch up)。我可以让 Xcode 为该特定事件生成一个虚拟处理程序函数吗?

采纳答案by LJ Wilson

Don't connect the segueto the button, connect the segue to the View Controller itself, then in the button's IBAction:

不要将 连接segue到按钮,将 segue 连接到视图控制器本身,然后在按钮的IBAction:

- (IBAction)myButtonTapped:(id)sender {
    // If you need to do some other work prior to firing the segue, do it here.

    [self performSegueWithIdentifier:@"MySegue" sender:nil];
}

Then do your prep work in the prepareForSegue method:

然后在 prepareForSegue 方法中做你的准备工作:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSString *identifier = segue.identifier;

    if ([identifier isEqualToString:@"MySegue"]) {
        // Do my stuff here
    }
}

I hardly ever connect seguesto UIButtons, UITableCells, etc, I connect them to my VC and then in the action, call the segue. This gives you more control.

我几乎从不连接seguesUIButtons、UITableCells 等,我将它们连接到我的 VC,然后在操作中调用 segue。这给了你更多的控制。

回答by Aluminum

If you're using Storyboard and segues to launch a new view using an UIButtonthe action that launches the new view is prepareForSegue:sender:. What network operation do you want to do before launching the view? For example if you need to know if the device is online or not beforelaunching the view and launch it onlyif the device has internet access you could also use shouldPerformSegueWithIdentifier:sender:and an if statementinside it.

如果您使用 Storyboard 和 segues 来启动新视图UIButton,则启动新视图的操作是prepareForSegue:sender:. 在启动视图之前,您要进行哪些网络操作?例如,如果你需要知道,如果该设备是在线还是不启动的观点,并启动它只是如果设备有互联网接入,你也可以使用shouldPerformSegueWithIdentifier:sender:if statement里面。

For example:

例如:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if ("The device has internet access")
    {
        return NO; // The view won't be launched
    }

    else
    {
        return YES; // We have internet, launch the view
    }
}

This will make the view launch able only when the device has internet access, if not anything will happen.

这将使视图仅在设备可以访问互联网时才能启动,否则不会发生任何事情。

回答by Hanief

On the view controller that holds the storyboard, implement this method :

在保存故事板的视图控制器上,实现此方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"YourSegueIdentifier"]) {
        // Implement your network operation here 

    } 
}

It will get executed before the segue happens.

它将在 segue 发生之前执行。