0

画像が入ったボタンがあります。ボタンを押すとポップアップが表示されます。ボタンが空で、画像が入っていない場合、すべてが機能しました。画像を追加したところ、ボタンが機能しなくなりました。ボタンはIBActionに対応し、IBOutletを介してXIBに接続されます。私に何ができる?

 - (void)viewDidLoad
 {      
      //.....
      self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];

     if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){

        self.myButton.frame = CGRectMake(160, 502, 73, 33);
       [self.myButton setImage:[UIImage imageNamed:@"iphone_legenda.png"] 
                                           forState:UIControlStateNormal];
        self.myButton.contentMode = UIViewContentModeScaleToFill;

     }else{

       self.myButton.frame = CGRectMake(330, 917, 182, 68);
       [self.myButton setImage:[UIImage imageNamed:@"ipad_legenda.png"] 
                                         forState:UIControlStateNormal];
       self.myButton.contentMode = UIViewContentModeScaleToFill;
     }


 [self.view addSubview:self.myButton]; 
 /*The problem arose in this line, without addSubview the image  you could not see 
  but the button worked, added addSuview the image you see but the button does not 
  work anymore*/

  NSLog(@"The button is %@", self.myButton);

 /*The button is <UIButton: 0x80f1e90; frame = (330 917; 182 68); opaque = NO; 
  layer = <CALayer: 0x80f1210>>*/

     //.......

 }

 -(IBAction)pressedButton:(id)sender{
      //This method initially worked
 }
4

1 に答える 1

1

ボタンがXIBですでに定義されている場合、コードはイベント(IBAction)がアタッチされていない別の個別のボタンを作成しています。

コードを次のように変更できます。

- (void)viewDidLoad
{      
  //.....

 if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){

    self.myButton.frame = CGRectMake(160, 502, 73, 33);
   [self.myButton setImage:[UIImage imageNamed:@"iphone_legenda.png"] 
                                       forState:UIControlStateNormal];
    self.myButton.contentMode = UIViewContentModeScaleToFill;

 }else{

   self.myButton.frame = CGRectMake(330, 917, 182, 68);
   [self.myButton setImage:[UIImage imageNamed:@"ipad_legenda.png"] 
                                     forState:UIControlStateNormal];
   self.myButton.contentMode = UIViewContentModeScaleToFill;
 }

}

他のオプションは、2つのXIBを持つことです。1つはiPad用、もう1つはiPhone用です。

于 2012-11-12T15:21:04.193 に答える