开发者

UIView Buttons loaded with XIB do not work

I've loaded a UIView (FirstView.m) with a separate XIB (SecondView.xib), but the buttons in that XIB crash the app. The code for the buttons (IBOutlet & IBAction) are in SecondView.m.

Do I need to point the code from SecondView.m to FirstView.m? I tried using #import and @class... but was unsuccessful.

The code I'm using is completely valid... I'm pretty sure the issue has something to do with the XIB being loaded into the UIView... and then possibly losing its connection to the implementation file. Thoughts?

Thanks in advance!

FirstView.h

#import <UIKit/UIKit.h>
@interface FirstView : UIViewController {
    IBOutlet UIView *SecondViewPopUP;
    IBOutlet UIButton *openBTN;
    }

@property (nonatomic, retain) UIView *SecondViewPopUP;
@property (nonatomic, retain) IBOutlet UIButton *openBTN;
-(IBAction)showPopUp:(id)sender;

FirstView.m

@synthesize SecondViewPopUP;
@synthesize openBTN

- (void)viewDidLoad {
    SecondViewPopUP.alpha = 0;

    // Add IncidentsViewController to view
    SecondView *SecondV=[[SecondView alloc] init];
    SecondV.view.frame = CGRectMake(0, 0, 262, 269);
    SecondV.view.clipsToBounds = YES;
  开发者_如何学Python  [SecondViewPopUP addSubview:SecondV.view];
    SecondViewPopUP.frame = CGRectMake(0, 76, 262, 269);
    [SecondV release];
    }

-(IBAction)showPopUp:(id)sender {
    NSLog(@"Stats Button was pressed");

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.4];

    SecondViewPopUP.alpha = 1;

    [UIView commitAnimations];
    }

SecondView.h

#import <UIKit/UIKit.h>

@interface ShareViewController : UIViewController {
    IBOutlet UIButton *share_facebook;
    IBOutlet UIButton *share_twitter;
    }

@property (nonatomic, retain) IBOutlet UIButton *share_facebook;
@property (nonatomic, retain) IBOutlet UIButton *share_twitter;

-(IBAction)shareOnFB:(id)sender;
-(IBAction)shareOnTwitter:(id)sender;

SecondView.m

@synthesize share_twitter, share_facebook;
- (void)viewDidLoad {
    [super viewDidLoad];
    }

-(IBAction)shareOnFB:(id)sender {
    NSLog(@"Shared on FB");
    }

-(IBAction)shareOnTwitter:(id)sender {
    NSLog(@"Shared on Twitter");    
    }


First of all FirstView (and presumably SecondView) is a UIViewController not a UIView so naming it "FirstViewController" would be much clearer. Views and view controllers are very different things.

Secondly you are adding a UIViewController's view as a subview of another view on the line "[SecondViewPopUP addSubview:SecondV.view];" That's not how UIViewControllers are expected to be used and the UIViewController programming guide recommends against it for good reason.

Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy. In iPhone applications, the views in a view hierarchy traditionally cover the entire screen, but in iPad applications they may cover only a portion of the screen. The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content.

Finally if you were to post the error listed when your app crashes we would probably see that you are attempting to send -shareOnFB: or -shareOnTwitter: messages to an instance of "FirstView" which does not implement them because your nib bindings are not configured appropriately ie you set the File's Owner of the nib to be "SecondView" and then loaded it with an instance of "FirstView" as its owner. Impossible to say for sure without more data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜