2013-01-12 57 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