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

时间:2017-11-11 15:38:46

标签: ios objective-c uiview uiviewcontroller delegates

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

编辑:

ActivityCard.h

@interface ActivityCard : UIView


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

@end

ActivityView.h

@interface ActivityView : UIView

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

@end

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

@interface HomeViewController : UIViewController

@property (strong) ActivityView *viewActivity;

@end

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"];

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

1 个答案:

答案 0 :(得分:1)

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

  • property
  • 中的所有文字字段创建Enter Button propertyActivityCard.h
  • 使用下面的代码在HomeViewController Enter Button添加操作。

    [self.activityView.cardView enterButton addTarget:self action:@selector(enterButtonDidTouch) forControlEvents:UIControlEventTouchUpInside];
    
  • 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
    }