按钮上的发送值从UIView单击到其superview的superview(UIViewController)-iOS

时间:2023-02-12 15:01:55

I have a ViewController called HomeViewController. Its subview is ActivityView (UIView). ActivityView's subview is ActivityCard (UIView). In ActivityCard, I have a button called Enter. On it's click I need to send the values of all textfields from ActivityCard xib to HomeViewController and call an API from there. I have never worked with xib's before so unable to manage this scenario. Any help would greatly be appreciated!

我有一个名为HomeViewController的ViewController。它的子视图是ActivityView(UIView)。 ActivityView的子视图是ActivityCard(UIView)。在ActivityCard中,我有一个名为Enter的按钮。单击它我需要将所有文本字段的值从ActivityCard xib发送到HomeViewController并从那里调用API。我之前从未使用过xib,因此无法管理这种情况。任何帮助将不胜感激!

EDIT:

编辑:

ActivityCard.h

ActivityCard.h

@interface ActivityCard : UIView


- (IBAction)actionEnterClick:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *lblActivityCount;
@property (weak, nonatomic) IBOutlet UIButton *enterButton;

@end

ActivityView.h

ActivityView.h

@interface ActivityView : UIView

@property (strong) ActivityCard *card;
@property (weak, nonatomic) IBOutlet iCarousel *carousel;

@end

ActivityView.m

ActivityView.m

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
if (view == nil)
    {
        _card = (ActivityCard*) [[[NSBundle mainBundle] loadNibNamed:@"ActivityCard" owner:nil options:nil] objectAtIndex:0];

        _card.frame = CGRectMake(0, 0, 280.0, 350.0);
        _card.layer.cornerRadius = 10.0f;
        view = _card;
    }
    return view;
}

HomeViewController.h

HomeViewController.h

@interface HomeViewController : UIViewController

@property (strong) ActivityView *viewActivity;

@end

HomeViewController.m

HomeViewController.m

_viewActivity = (ActivityView*) [[[NSBundle mainBundle] loadNibNamed:@"ActivityView" owner:nil options:nil] objectAtIndex:0];
    _viewActivity.frame = CGRectMake(0.0f, 0.0f, _viewContainer.frame.size.width, _viewContainer.frame.size.height);
    [_viewActivity updateActivityUI];

    for (UIView *child in _viewContainer.subviews) {
        [child removeFromSuperview];
    }
    [_viewContainer addSubview:_viewActivity];

    [_viewActivity.card.enterButton addTarget:self action:@selector(enterButtonDidTouch) forControlEvents:UIControlEventTouchUpInside];

    [_viewActivity.card.lblActivityCount setText:@"11"];

This code has been written when we need to load the ActivityView on a button click.

当我们需要在按钮单击时加载ActivityView时,已编写此代码。

1 个解决方案

#1


1  

Have many ways to do it, i will show you a way which i think it's the easiest way.

有很多方法可以做到,我会告诉你一种方式,我认为这是最简单的方法。

  • Create a property of Enter Button and property for all your textfields in ActivityCard.h
  • 在ActivityCard.h中为所有文本字段创建Enter Button属性和属性
  • In HomeViewController add action for your Enter Button by using my code below.

    在HomeViewController中,使用下面的代码为Enter按钮添加操作。

    [self.activityView.cardView enterButton addTarget:self action:@selector(enterButtonDidTouch) forControlEvents:UIControlEventTouchUpInside];
    
  • Create a method in HomeViewController.m name enterButtonDidTouch

    在HomeViewController.m中创建一个名为enterButtonDidTouch的方法

    - (void)enterButtonDidTouch {
        NSString *textOfTextField1 = self.activityView.activityCard.textField1.text;
        // With the same way you can get others text of text fields and use them
    }
    

#1


1  

Have many ways to do it, i will show you a way which i think it's the easiest way.

有很多方法可以做到,我会告诉你一种方式,我认为这是最简单的方法。

  • Create a property of Enter Button and property for all your textfields in ActivityCard.h
  • 在ActivityCard.h中为所有文本字段创建Enter Button属性和属性
  • In HomeViewController add action for your Enter Button by using my code below.

    在HomeViewController中,使用下面的代码为Enter按钮添加操作。

    [self.activityView.cardView enterButton addTarget:self action:@selector(enterButtonDidTouch) forControlEvents:UIControlEventTouchUpInside];
    
  • Create a method in HomeViewController.m name enterButtonDidTouch

    在HomeViewController.m中创建一个名为enterButtonDidTouch的方法

    - (void)enterButtonDidTouch {
        NSString *textOfTextField1 = self.activityView.activityCard.textField1.text;
        // With the same way you can get others text of text fields and use them
    }