gpt4 book ai didi

ios - 从 Nib 加载的 subview 中的文本字段访问文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:28:58 25 4
gpt4 key购买 nike

我正在尝试让我的 viewcontroller (PersonalListViewController) 加载一个 UITableView。页面底部有一个 addPersonalItem 按钮。当用户按下按钮时,它会从 Nib 文件加载 UIView (AddItemView)。

AddItemView 上有两个 UITextFields。 1) 如何从 PersonalListViewController 访问字段中输入的文本以及 2) PersonalListViewController 如何知道用户何时删除了 AddItemView从屏幕(即在 AddItemView 上输入文本并按下 Done)?

在PersonalListViewController.m

- (IBAction)addPersonalItem:(id)sender {

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"AddItemView" owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self.view addSubview:mainView];
}

在 AddItemView.h 中

#import <UIKit/UIKit.h>

@interface AddItemView : UIView {
IBOutlet UITextField *itemNameTextField;
IBOutlet UITextField *quantityTextField;
}

@property (strong, nonatomic) IBOutlet UITextField *itemNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *quantityTextField;

@end

在AddItemView.m

#import "AddItemView.h"

@implementation AddItemView

@synthesize itemNameTextField;
@synthesize quantityTextField;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}

最佳答案

试试这个,通过

AddItemView 中定义一个 block
typedef void (^completionBlock)(NSString *itemName, NSString *quantity);

为这个 block 添加一个属性

@property (nonatomic, strong) completionBlock completionCallback;

所以,在AddItemView.h

#import <UIKit/UIKit.h>

typedef void (^completionBlock)(NSString *itemName, NSString *quantity);

@interface AddItemView : UIView {
IBOutlet UITextField *itemNameTextField;
IBOutlet UITextField *quantityTextField;
}

@property (strong, nonatomic) IBOutlet UITextField *itemNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *quantityTextField;
@property (nonatomic, strong) completionBlock completionCallback;

- (IBAction)completionButtonAction:(id)sender;

@end

AddItemView 中添加一个 UIButton 并在它的操作中实现一个像这样的 block

- (IBAction)completionButtonAction:(id)sender {
if (self.completionCallback != nil) {
NSString *itemName = self.itemNameTextField.text;
NSString *quantity = self. quantityTextField.text;
self.completionCallback (itemName, quantity);
}
}

在 PersonalListViewController.m 中

- (IBAction)addPersonalItem:(id)sender {

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"AddItemView" owner:self options:nil];
AddItemView *mainView = (AddItemView *)[subviewArray objectAtIndex:0];
void (^completionBlock)(NSString *itemName, NSString *quantity) = ^(NSString *itemName, NSString *quantity){
[self personalItemAddedWithItemName:itemName quantity:quantity];
};
mainView.completionCallback = completionBlock
[self.view addSubview:mainView];
}


- (void)personalItemAddedWithItemName:(NSString *)itemName quantity:(NSString *)quantity {
//add your code here
}

关于ios - 从 Nib 加载的 subview 中的文本字段访问文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21104877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com