带有UITextFieldDelegate问题的模型视图控制器 [英] Model View Controller with UITextFieldDelegate issue

查看:40
本文介绍了带有UITextFieldDelegate问题的模型视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些非常简单的事情,而且我认为我犯了一个小错误,我可能只是在这里看不到.我在MVC之间进行了此操作,以简单地将名称从 AddViewController 传输到 MasterViewController ,并将其添加到 MasterController 的<代码> UITableView .虽然,我已经将所有步骤的所有步骤都"NSLogged"了,但我基本上从中得到的是输入到名称" UITextField 中的文本本身并未传递放入我的 NSString 方法 _aname 中.

I'm working with something really easy, and I think I'm making a small mistake that I'm probably just not seeing here. I have this going on between my MVC to simply transfer a name from the AddViewController to the MasterViewController, and add it to the MasterController's UITableView. Although, I have "NSLogged" everything down to each step of the process, and what I'm basically getting out of it is that the text that's inputted into the "name" UITextField isn't passing itself into my NSString method, _aname.

还有一点,这是可行的,我在将名称从 AddViewController 设置为 MasterViewController 时遇到了麻烦.因此,如果任何人也可以仔细研究一下这一部分,并确保我正确设置了所有内容,我将非常感激.

Also, at one point, this was working and I was having trouble with setting the name to the MasterViewController from the AddViewController. So, if anyone could just give that part a look over too and make sure I have everything set up properly, I would hugely appreciate it.

简而言之,它看起来像这样.

In a nutshell, it looks like this.

MasterViewController.h

MasterViewController.h

@property (nonatomic, strong) AddViewController *addViewController;

MasterViewController.m

MasterViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [self.Name setName:name];
    NSLog(@"%@", cell.textLabel.text);

    return cell;

Name.h

- (NSString *)setName:(NSString *)name;

Name.m

- (NSString *)setName:(NSString *)name
{
    [_nameStack addObject:name];

    NSLog(@"Name set to: %@", name);

    return name;
}

AddViewController.m

AddViewController.m

- (void)viewDidLoad
{

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(finalizeAdditionOfName:)];

    NSUserDefaults *nameDefault = [NSUserDefaults standardUserDefaults];
    nameInput.text = [nameDefault objectForKey:@"Name"];
}

- (IBAction)finalizeAdditionOfName:(id)sender
{
    _name = [[Name alloc] init]
    nameInput.text = _name;
    [_name setName:_aname];
    NSLog(@"Name set to (AddViewController): %@", _aname);
    [self dismissViewControllerAnimated:YES completion:NULL];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 0) { // Section 1
        if ([indexPath row] == 0) {
            nameInput.delegate = self;
            [nameInput setEnabled: YES];
            [cell addSubview:nameInput];
            [nameInput becomeFirstResponder]
            nameInput.text = _aname;
            NSUserDefaults *nameDefault = [NSUserDefaults standardUserDefaults];
            [nameDefault setObject:_aname forKey:@"Name"];
    }
}

推荐答案

如果我的评论对我们没有帮助,我制作了一个示例项目,我认为该示例显示了如何将数据从文本字段获取到表格视图.告诉我您是否有任何疑问:

if my comment doesn't turn out to be helpful, I made a sample project that I think shows how you might get the data from the text field to the table view. Tell me if you have any questions about it:

https://github.com/MaxGabriel/TextField-TableView-Example

这篇关于带有UITextFieldDelegate问题的模型视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆