2013-01-12 23 views
0
的UIView的

所以,你好,我想,如果我在做 [self createGameView];与线加载的UIViewController 不同UIViews这样 self.view=someView;异常而更换的UIViewController

所以这里的一些代码

- (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]; 
} 

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: 这里创建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; 
} 
+0

只在 - (void)loadView not in viewDidLoad中设置self.view –

+0

仅禁用此行[Button setTag:[[[NSString alloc] initWithFormat:@“%d” ,i] intValue]]; &再试 –

+0

检查我的可编辑答案 –

回答

0

我是多么愚蠢看到这个刚才而现在随着UINavigationToolbar做同样的事情,并swaping整体的UIViewController 的问题是,我给了菜单按钮的游戏按钮功能造成的错误因为改变了尚未创建的游戏视图

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