我有一个名为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时,已编写此代码。

最佳答案

有很多方法可以做到,我将向您展示我认为这是最简单的方法。


propertyEnter 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
}

关于ios - 将按钮单击上的值从UIView发送到其 super View 的 super View (UIViewController)-iOS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47239660/

10-09 13:48