0

こんにちは私はこのような行でUIViewControllerに別のUIViewをロードしようとしています 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で実行する場合は問題ありません が、ボタンからの場合は例外ヘルプを取得してください

それは私が出力で得るものです-それが役立つかどうかはわかりませんl

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:ここにgameView 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

UINavigationToolbar を使用して同じことを実行し、UIViewController 全体を交換しているときに、今これを見たときの愚かさは、まだ作成されていないゲーム ビューを変更したためにエラーを引き起こすメニュー ボタンにゲーム ボタン機能を与えたことでした。

于 2013-01-15T16:38:45.943 に答える
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 に答える