0

所以你好我正在尝试在 UIViewController 中加载不同的 UIViews 像这样的行 self.view=someView;

所以这里有一些代码

- (void)viewDidLoad
{
    [self createMenuView];
}

- (void)createMenuView
{
    UIView *viewMenu=[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    viewMenu.backgroundColor=[UIColor grayColor];

    for(int i=0;i<3;i++){
        UIButton *Button=[UIButton buttonWithType:UIButtonTypeCustom];
        [Button setFrame:CGRectMake(0,0,320,80)];//creating it's frame
        [Button setCenter:CGPointMake(160,i*80+200)];//now place frame where we need
        [Button setTitle:[[NSString alloc] initWithFormat:@"%d",i] forState:UIControlStateNormal];//write k to title
        [Button setTag:[[[NSString alloc] initWithFormat:@"%d",i] intValue]];//write xy to tag
        [Button addTarget:self action:@selector(gameButtonClicked:) forControlEvents:UIControlEventTouchUpInside];//-event
        [Button setBackgroundImage:[UIImage imageNamed:@"blue.jpg"] forState:UIControlStateNormal];//set background
        [Button setAdjustsImageWhenHighlighted:FALSE];//disable tinting buttons while selected
        [viewMenu addSubview:Button];
    }
    self.view=viewMenu;
}

- (void)createGameView
{
    //here is just creation of a gameView which get's a whole lots of buttons
}

- (void)menuButtonClicked:(UIButton*)button
{
    [self createGameView];
}

如果我 [self createGameView];在 viewDidLoad 中这样做,那没关系,但如果它来自按钮,那么我会得到异常帮助

这就是我在输出中得到的 - 不确定它是否有帮助

013-01-12 18:54:19.681 Barley Break[11235:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
*** First throw call stack:
(0x1c90012 0x10cde7e 0x1c320b4 0x4963 0x47d2 0x10e1705 0x18920 0x188b8 0xd9671 0xd9bcf 0xd8d38 0x4833f 0x48552 0x263aa 0x17cf8 0x1bebdf9 0x1bebad0 0x1c05bf5 0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x1565c 0x242d 0x2355 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

UPD1:尝试使用[self setView:someView];而不是[self.view=someView];- 相同的异常

UPD2:这里是创建游戏视图 ps:randExcludable - 只是 arc4random()

- (void)createGameView
{
    UIView *viewGame=[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    viewGame.backgroundColor=[UIColor grayColor];

    int k=0;
    int n=4;
    int t=0,q=0;
    NSMutableArray *yEx=[[NSMutableArray alloc] initWithObjects:[[NSString alloc] initWithFormat:@"%d",5], nil],*xEx=[[NSMutableArray alloc] initWithObjects:[[NSString alloc] initWithFormat:@"%d",5], nil];

    for(int i=0;i<4;i++){
        t=[self randExclludable:n arrayToExclude:yEx];
        for(int j=0;j<4;j++){
            q=[self randExclludable:n arrayToExclude:xEx];
            UIButton *Button=[UIButton buttonWithType:UIButtonTypeCustom];//Creating button of custom type
            [Button setFrame:CGRectMake(0,0,80,80)];//creating it's frame
            [Button setCenter:CGPointMake(q*80+40,t*80+120)];//now place frame where we need
            [Button setTag:[[[NSString alloc] initWithFormat:@"%d",(t*10)+q] intValue]];//write xy to tag
            [Button setTitle:[[NSString alloc] initWithFormat:@"%d",k] forState:UIControlStateNormal];//write k to title
            [Button addTarget:self action:@selector(gameButtonClicked:) forControlEvents:UIControlEventTouchUpInside];//-event
            [Button setBackgroundImage:[UIImage imageNamed:@"blue.jpg"] forState:UIControlStateNormal];//set background
            if(i==0 && j==0){
                [Button setBackgroundImage:[UIImage imageNamed:@"gray.jpg"] forState:(UIControlStateNormal | UIControlStateDisabled)];//special background for the zero button
                [Button setEnabled:NO];//disable zero button
                [Button setTitle:@"" forState:UIControlStateNormal];//and set title of zero button to ""
            }
            [Button setAdjustsImageWhenHighlighted:FALSE];//disable tinting buttons while selected
            //[self.view addSubview:Button];//creating subview on the desired view
            //[self.buttons16 addObject:Button];//place button in array
            [viewGame addSubview:Button];
            Button=nil;
            k++;
            [xEx addObject:[[NSString alloc] initWithFormat:@"%d",q]];
        }
        [xEx removeAllObjects];
        [yEx addObject:[[NSString alloc] initWithFormat:@"%d",t]];
    }
    self.view=viewGame;
    //[self setView:viewGame];
    //[self.view addSubview:viewGame];
    viewGame=nil;
}
4

2 回答 2

0

用这个

     [Button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];//write k to title
     [Button setTag:i];//write xy to tag

而不是这个

    [Button setTitle:[[NSString alloc] initWithFormat:@"%d",i] forState:UIControlStateNormal];//write k to title
    [Button setTag:[[[NSString alloc] initWithFormat:@"%d",i] intValue]];//write xy to tag
于 2013-01-12T17:23:07.073 回答
0

刚才我看到这个有多愚蠢,现在用 UINavigationToolbar 做同样的事情并交换整个 UIViewController 问题是我给了菜单按钮游戏按钮功能,由于更改尚未创建的游戏视图而导致错误

于 2013-01-15T16:38:45.943 回答