objective-c - 我可以在xCode中将按钮链接到随机 View Controller 吗?

标签 objective-c xcode ipad random ios-simulator

我对此很陌生.....抱歉。我已经用xCode创建了一个包含10个 View Controller 的应用程序。每个 View Controller 都有一个图像 View 和一个圆角rect按钮。

我想进行设置,以便在 View 上按按钮可以选择其他9个 View 之一,但是加载的 View 必须是随机的。

到目前为止,我所做的是:

-我在 Storyboard 上放置了10个 View Controller ,并将每个类的类别设置为scene1-10

-我创建了10个名为Scene1-10的Objective-C类-ViewController(.h和.m文件)

-在这个时尚场景中我已经在它们之间创建了segue1-> scene2-> scene3-> scene4
-> scene5-> scene6-> 7scene-> scene8-> scene9-> scene10-> scene1

-我将每个segue的类别设置为segue1-10,在他们要去的场景之后命名
到(场景1和场景2之间的segue是segue2)

-我使所有.h文件看起来都像这样(场景#随文件而异):

#import <UIKit/UIKit.h>

@interface scene1 : UIViewController

- (IBAction) randomButton;

@end

-我使所有.m文件看起来都像这样(场景#视 View 而异
#import "scene1.h"

@interface scene1 ()

@end

@implementation scene1

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction) randomButton : (id) sender
{
    int i = arc4random() % 10;
    if(i == 1)
    {
        [self performSegueWithIdentifier: @"segue1" 
                                  sender: self];
    }
    else if(i == 2)
    {
        [self performSegueWithIdentifier: @"segue2" 
                              sender: self];
    }
    else if(i == 3)
    {
        [self performSegueWithIdentifier: @"segue3" 
                                  sender: self];
    }
    else if(i == 4)
    {
        [self performSegueWithIdentifier: @"segue4" 
                                  sender: self];
    }
    else if(i == 5)
    {
        [self performSegueWithIdentifier: @"segue5" 
                                  sender: self];
    }
    else if(i == 6)
    {
        [self performSegueWithIdentifier: @"segue6" 
                                  sender: self];
    }
    else if(i == 7)
    {
        [self performSegueWithIdentifier: @"segue7" 
                                  sender: self];
    }
    else if(i == 8)
    {
        [self performSegueWithIdentifier: @"segue8" 
                                  sender: self];
    }
    else if(i == 9)
    {
        [self performSegueWithIdentifier: @"segue9" 
                                  sender: self];
    }
    else
    {
        [self performSegueWithIdentifier: @"segue10" 
                                  sender: self];
    }
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

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

@end

-我将为每个 View Controller 列出的接收到的动作“randomButton”链接到 View Controller 上的按钮

怎么了:

我在每个.m文件的“@implementation scene1”行上收到“未完成实现”警告,但该应用程序确实会生成和加载。

加载后,如果按此按钮,应用程序将冻结,并弹出xCode的调试窗口,并显示以下内容:
2012-05-05 07:11:11.789 randomTester[2448:f803] -[scene1 randomButton]: unrecognized                     selector sent to instance 0x686d370
2012-05-05 07:11:11.792 randomTester[2448:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[scene1 randomButton]: unrecognized selector sent to instance 0x686d370'
*** First throw call stack:
(0x13cc022 0x155dcd6 0x13cdcbd 0x1332ed0 0x1332cb2 0x13cde99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbed8a 0x3e2cf 0x3e5e6 0x24dc4 0x18634 0x12b6ef5 0x13a0195 0x1304ff2 0x13038da 0x1302d84 0x1302c9b 0x12b57d8 0x12b588a 0x16626 0x1f5d 0x1ec5)
terminate called throwing an exception(lldb)

我意识到我可能完全错了。我搜索了所有内容,但找不到我真正需要的东西,因此我不得不将相似代码中的位拼凑起来。

感谢您宝贵的时间和协助!



更新

okt第二轮:

你们真是太棒了,我一直没意识到我是通过IBAction的声明和实现来做到这一点的。修复了所有实施错误。但是,我似乎有一个潜在的问题。

我的.h文件现在看起来像这样:
#import <UIKit/UIKit.h>

@interface scene1 : UIViewController

- (IBAction) randomButton;

@end

我的.m苍蝇现在看起来像这样:
#import "scene1.h"

@interface scene1 ()

@end

@implementation scene1

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)randomButton
{
    int i = arc4random() % 10 + 1;
    NSString *segueIdentifier = [NSString stringWithFormat:@"segue%d", i];
    [self performSegueWithIdentifier:segueIdentifier sender:self];

}- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

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

@end

没有实现错误,并且构建良好,但是如果在我按按钮加载后,该应用将冻结,并且xCode的调试窗口将弹出并显示以下内容:
2012-05-05 13:00:09.236 randomTester[2699:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:     'Receiver (<scene1: 0x6868f90>) has no segue with identifier 'segue5''
*** First throw call stack:
(0x13cb022 0x155ccd6 0xdd61b 0x2592 0x13cce99 0x1814e 0x180e6 0xbeade 0xbefa7 0xbdd8a 0x3d2cf 0x3d5e6 0x23dc4 0x17634 0x12b5ef5     0x139f195 0x1303ff2 0x13028da 0x1301d84 0x1301c9b 0x12b47d8 0x12b488a 0x15626 0x224d 0x21b5)
terminate called throwing an exception(lldb) 

显然,该应用程序正在查看scene1 View Controller ,而未看到“segue5”(这是新IBAction调用的随机segue)。这是因为Scene1 View Controller 上没有segue5,因为它只有一个segue进入了Scene2,如我在上面的问题的第一部分中所述。

为了解决这个问题,我在场景1上创建了10个segues类,其中包含segue1-10类。我分别将场景2-10和场景2-10连接起来。该按钮可以调用segue1是合理的,因为我不是随机的,因此可以指向自身。由于无法创建从场景1到场景1的序列,因此我创建了场景2的第二个序列,并给它提供了一类segue1。

没有错误,应用程序已加载,但按下按钮会导致应用程序冻结,这在调试中显示如下:
2012-05-05 13:45:04.467 randomTester[2803:f803] -[scene1 randomButton:]: unrecognized selector sent to instance 0x68b3760
2012-05-05 13:45:04.479 randomTester[2803:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:     '-[scene1 randomButton:]: unrecognized selector sent to instance 0x68b3760'
*** First throw call stack:
(0x13cb022 0x155ccd6 0x13cccbd 0x1331ed0 0x1331cb2 0x13cce99 0x1814e 0x180e6 0xbeade 0xbefa7 0xbdd8a 0x3d2cf 0x3d5e6 0x23dc4     0x17634 0x12b5ef5 0x139f195 0x1303ff2 0x13028da 0x1301d84 0x1301c9b 0x12b47d8 0x12b488a 0x15626 0x224d 0x21b5)
terminate called throwing an exception(lldb) 

问题一:
知道如何建立这些序列才能完成这项工作吗?

问题二:
会更好:

-创建10个分别名为image1-10的 View Controller ,每个 Controller 带有一个图像 View 和一个按钮

-在每个按钮上设置按钮,以启动到名为sceneRandomizer的第11个 View Controller
(我不会给这些segues类)

-从sceneRandomizer连接10个场景到场景1-10
( class 为segue1-10)

-在“sceneRandomizer”中编写代码以自动从1-10开始随机选择
组非常像重定向(我不知道如何写....只是扔在那里)

从理论上讲,这将缓解我遇到的第二个错误,即在调用它的 View Controller 上不存在segue调用的地方。

如果这种替代设置可行,是否会造成从一个选择到另一个这样的重大滞后?



更新

现在正在工作

终于让它起作用了。这似乎是一种“环回”方法,并且我敢肯定有一种更有效且可以接受的方法来做到这一点,但这就是我所做的:

.h和.m中的代码与上一次尝试相同,但我重申一下只是为了清除:

我的.h文件如下所示:
#import <UIKit/UIKit.h>

@interface scene1 : UIViewController

- (IBAction) randomButton;

@end

我的.m文件如下所示:
#import "scene1.h"

@interface scene1 ()

@end

@implementation scene1

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)randomButton
{
    int i = arc4random() % 10 + 1;
    NSString *segueIdentifier = [NSString stringWithFormat:@"segue%d", i];
    [self performSegueWithIdentifier:segueIdentifier sender:self];

}- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

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

@end

我做了些什么:

好的,这次
-我将11个 View Controller 拖到 Storyboard 上
  • 我将一个命名为“startPage”,将其他命名为“scene1-10”
  • 我创建了1个.h和一个.m文件(编码如上所示),并将它们命名为StartPage.h和
    StartPage.m
  • 我将 Storyboard上的所有11个 View 类都设置为一个 View Controller (StartPage)
  • 我做了10次选择,其中1次是从StartPage到10个场景中的每个场景,然后是
  • 我从场景1进行了10次测试,从场景1到其他9个场景分别进行了一次测试,并给了他们segue2-10类(以他们进入的场景命名)
    note备注
    因为无法创建从场景1到场景1的序列,所以我从场景1到场景2进行了第二个序列,只是给它提供了一个segue1类。
  • 我为其他9个场景中的每一个重复了最后一步

  • 这确实有效。它的构建和运行没有错误。 StartPage首先出现。击中按钮将从组中加载一个随机 View (场景1-10),在结果 View 上单击按钮将从组中随机加载另一个 View ,依此类推。

    这样做的不利方面是建立所有标记所需的时间。现在,此 Storyboard 具有110个segue,仅控制11个 View 之间的移动。 .....那是我生命中的一个小时,我再也回不来了。

    对于像我这样的新手:

    如果选择使用此方法,则在命名segue标识符时请非常。假设您为场景5布置了segues,但您错误地将2个segues segue2命名为,而忘记命名为一个segue3。如果最终用户在scene5上并且随机按钮调用segue3,则该应用程序将崩溃,因为Scene5没有名为segue3的segue。我之所以仅提及这一点,是因为在测试过程中很难发现这样的错误,因为除非您在scene5上碰巧调用segue3,否则一切似乎都可以正常工作。

    如果有人发现使用此方法的任何缺点(我绝对不知道),请提出。如果您有更好的方法,我将不胜感激:)

    最佳答案

    randomButton方法的声明和实现应具有相同的签名。因此,您应该在.h文件中这样声明它:

    - (IBAction)randomButton:(id)sender;
    

    .m文件中更改它,如下所示:
    - (IBAction)randomButton
    {
        int i = arc4random() % 10;
        // ...
    }
    

    另外,您可以使用i的值生成segue标识符,从而简化代码:
    - (IBAction)randomButton
    {
        int i = arc4random() % 10 + 1;
        NSString *segueIdentifier = [NSString stringWithFormat:@"segue%d", i];
        [self performSegueWithIdentifier:segueIdentifier sender:self];
    }
    

    另外,请注意,我将生成i的行更改为将值从1更改为10。您的原始代码会生成从09的值。

    关于objective-c - 我可以在xCode中将按钮链接到随机 View Controller 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10461839/

    相关文章:

    ios - 按钮在 iOS6 中隐藏文本

    ios - 带有额外空间的 UITableViewController 滚动(如 Notes 应用程序)

    jquery - 确定触摸移动的垂直方向

    iphone - TableView 弹出框未解除 [self dismissPopoverAnimated :YES];

    ios - 在 iPad 上的 Safari 中,无限滚动的网站突然重新加载并显示错误消息

    ios - 在 iOS Xcode 的 UIView 上移动图像

    ios - NSDateFormatter 提前一天

    swift - 从 nib 实例化 UIView 导致无限循环问题

    ios - 无法从collectionView传递indexpath(到LightBox包中)

    ios - Xcode:从屏幕上删除文本数组