Custom UIView loaded from Xib

前端 未结 3 363
北海茫月
北海茫月 2020-12-11 06:29

I have created a custom subclass of UIView along with a xib file and declared IBOutlets and IBActions within the custom class.

@interface ContactUsView : UIV         


        
相关标签:
3条回答
  • 2020-12-11 06:55

    If you are loading a UIView for an xib then you should create a class method to load the view.

    In your customview.h

    +(id)customView;
    

    & in your customview.m

    + (id)customView
    {
        CustomView *customView = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil] lastObject];
        if ([customView isKindOfClass:[CustomView class]])
            return customView;
        else
            return nil;
    }
    

    You can initialize it anywhere using:

    CustomView *myView = [CustomView customView];
    

    EDIT: Make sure you have changed your customview's class in identity inspecter & also make sure your connection of IBActions are with that class' methods.enter image description here

    enter image description here

    0 讨论(0)
  • 2020-12-11 06:58

    You can use delegate for this this is how you can do this

            @protocol CustomViewDelegate <NSObject>
    - (void)callButtonPressed:(id)sender;
    - (void)emailButtonPressed:(id)sender;
    - (void)displayCloseButtonPressed:(id)sender;
    
    @end
    
    @interface ContactUsView : UIView
    
    @property (nonatomic, weak) IBOutlet UIButton *displayCloseButton;
    
    @property (nonatomic, weak) id<CustomViewDelegate> ButtonDelegate;
    
    - (IBAction)callButtonPressed:(id)sender;
    
    - (IBAction)emailButtonPressed:(id)sender;
    - (IBAction)displayCloseButtonPressed:(id)sender;
    
    
    @end
    

    and in .m file

    - (IBAction)callButtonPressed:(id)sender
    {
    [self.ButtonDelegate callButtonPressed:sender];
    }
    
    - (IBAction)emailButtonPressed:(id)sender{
    [self.ButtonDelegate emailButtonPressed:sender];
    }
    
    - (IBAction)displayCloseButtonPressed:(id)sender{
    [self.ButtonDelegate displayCloseButtonPressed:sender];
    }
    

    After that just set the delegate with viewcontroller refrence and use those delegate here

    - (void)viewWillAppear:(BOOL)animated
    

    {

    ContactUsView *contactUs = [[ContactUsView alloc] initWithFrame:CGRectZero];
    contactUs.ButtonDelegate = self;
    
    CGPoint origin = self.view.frame.origin;
    CGSize size = self.view.frame.size;
    [contactUs setFrame:CGRectMake(origin.x,
                              CGRectGetMaxY(self.view.frame) - 100,
                              size.width,
                              contactUs.frame.size.height)];
    
    [self.view addSubview:contactUs];
    

    }

    - (void)callButtonPressed:(id)sender
    

    {}

    - (void)emailButtonPressed:(id)sender
    

    {}

    - (void)displayCloseButtonPressed:(id)sender
    

    {}

    I have done this and works totlly fine

    0 讨论(0)
  • 2020-12-11 07:01

    • Files owner = to my custom class

    Wrong. Files owner should be empty. The view itself is files owner. It means that you should connect all actions and outlets with ContactUsView in your xib.

    [[NSBundle mainBundle] loadNibNamed:@"ContactUsView" owner:self options:nil]

    ...

    self = (ContactUsView *)object;

    After you passed self as ownerparameter. You changing it. Which means that previously allocated ContactUsView (self) will be destroyed since -loadNibNamed:owner:options: do not retain it. If you apply my first advice you should send nil as owner parameter

    forloop here is not necessary use just array[0], because this is always your view if you have valid views hierarchy in your xib

    0 讨论(0)
提交回复
热议问题