0

现在,我有一个带有添加更多按钮的 tableViewController。当点击按钮时,会出现一个标题为“新习惯”的新单元格。点击单元格时,会出现一个带有文本字段的 DetailViewController。点击文本字段时,会显示一个 UIPicker,其中包含三个选项。First is "Posture", second is "Palaudies Abbs", and The third option is custom, which when custom is selected the keyboard is replaced for a keyboard. 使用键盘输入自定义习惯,它会显示在文本字段中。When Posture or palaudies is selected, the tableViewCell is named to the selection (which you can see how in the code). 现在的问题是将自定义选项设置为单元格的标题。目前我已经能够使用字符串,并尝试像其他人一样将其设置为 deleage,

这是我的代码

DetailViewController.h

#import <UIKit/UIKit.h>

@protocol DetailViewDelegate <NSObject>
- (void)setCellName2:(NSString *)cellName;
@end

@interface DetailViewController : UIViewController<UIPickerViewDelegate> {
}
@property (nonatomic, strong) id <DetailViewDelegate> delegate;
@property (nonatomic, strong) UIToolbar *toolBar;
@property (nonatomic, strong) UIBarButtonItem *backButton;
@property (nonatomic, strong) UIPickerView *Picker;
@property (nonatomic, strong) UIBarButtonItem *barDoneButton;
@property (nonatomic, strong) UIBarButtonItem *flexSpace;
@property (nonatomic, strong) NSString *customHabit;
@property (nonatomic, strong) NSString *cellNames;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *doneButton;
@property (nonatomic, strong) IBOutlet UITextField *habitField;
- (IBAction)backToRoot:(id)sender;
@end

.m

#import "DetailViewController.h"

#import "HabitViewController.h"

@interface DetailViewController ()

@property (retain, nonatomic) NSArray *pickerData;

@end

@implementation DetailViewController

@synthesize  Picker, toolBar, backButton, barDoneButton, flexSpace, cellNames;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.pickerData = @[@"Posture",@"Paludies Abbs",@"Custom"];

    toolBar = [[UIToolbar alloc] init];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    [toolBar sizeToFit];

    [toolBar setBackgroundImage:[UIImage imageNamed:@"red_navigation_bar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

    flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                              target:self
                                                              action:nil];
    // Done button on toolbar
    barDoneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                  target:self
                                                                  action:@selector(releasePicker)];
    // Back button on toolbar

    backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"style:UIBarButtonItemStyleDone
                                                 target:self
                                                 action:@selector(backToPicker)];

    // Habit PickerView

    Picker = [[UIPickerView alloc] init];
    Picker.showsSelectionIndicator = YES;
    Picker.delegate = self;
    barDoneButton.image = [UIImage imageNamed:@"button.png"];

    // Toolbar above picker

    [toolBar setItems:@[flexSpace, barDoneButton] animated:YES];

    self.habitField.inputAccessoryView = toolBar;

    [self.habitField addTarget:self action:@selector(customHabitChanged) forControlEvents:UIControlEventEditingDidEnd];

    [self.habitField setInputView:Picker];

    cellNames = @"New Habit";

}
- (void)customHabitChanged {
    self.customHabit = self.habitField.text;
    cellNames = self.customHabit;
}
- (void)backToPicker {
    [toolBar setItems:@[flexSpace, barDoneButton] animated:YES];
    [self.habitField resignFirstResponder];
    [self.habitField setInputView:Picker];
    [self.habitField becomeFirstResponder];
}
- (void)releasePicker {
    [self.habitField resignFirstResponder];
    [self.habitField setInputView:Picker];
    [toolBar setItems:@[flexSpace, barDoneButton] animated:YES];
}

- (IBAction)backToRoot:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.pickerData.count;
}


-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.pickerData[row];
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    cellNames = [[NSString alloc] init];
    int select = row;
    if (select == 0) {
        cellNames = @"Posture";

        [self.delegate setCellName2:cellNames];

    }
    if (select == 1) {

        self.habitField.text = @"Palaudies Abbs";

        cellNames = @"Palaudies Abbs";


    }
    if (select == 2) {

        cellNames = @"Custom";

        [self.habitField resignFirstResponder];
        [self.habitField setInputView:nil];
        [self.habitField becomeFirstResponder];
        [toolBar setItems:@[backButton, flexSpace, barDoneButton] animated:YES];
        self.habitField.text = nil;
        self.habitField.placeholder = @"Custom";
        [self.delegate setCellName2:cellNames];

    }
}

@end

习惯视图控制器.h

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface HabitViewController : UITableViewController <DetailViewDelegate> {
}

@property (strong, nonatomic) NSIndexPath *selectedCell;

@end

.m

#import "HabitViewController.h"

#import "DetailViewController.h"

@interface HabitViewController () {
    NSMutableArray *myCells;
}
@property(strong, nonatomic) NSString *cellName2;


@end

@implementation HabitViewController

@synthesize selectedCell;

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    [self.editButtonItem setTintColor:[UIColor colorWithRed:.33 green:.33 blue:.33 alpha:1]];

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
    addButton.tintColor = [UIColor colorWithRed:.33 green:.33 blue:.33 alpha:1];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar.png"] forBarMetrics:UIBarMetricsDefault];



}
- (void)viewDidAppear:(BOOL)animated {

    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)insertNewObject:(id)sender
{
    if (!myCells) {
        myCells = [[NSMutableArray alloc] init];
    }
    [myCells insertObject:@"New Habit" atIndex:0];
    NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return myCells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = myCells[indexPath.row];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [myCells removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
    }
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    DetailViewController *vc = segue.destinationViewController;
    vc.delegate = self;
}
#pragma mark - DetailViewDelegate

-(void)setCellName2:(NSString *)cellName {
    NSInteger selectedRow = [self.tableView indexPathForSelectedRow].row;
    NSLog(@"%@", cellName);
    [myCells replaceObjectAtIndex:selectedRow withObject:cellName];
    [self.tableView reloadData];
}

@end
4

1 回答 1

0

看起来委托是在文本编辑开始而不是结束时被调用的。在编辑完成之前,cellName 参数中不会有任何有价值的东西。

在自定义情况下,您必须使 textField 成为第一响应者,然后在用户完成编辑时做出反应。

要捕获该事件,请将 DetailVC 作为文本字段的委托,并响应textFieldDidEndEditing. 在该方法中,使用 textField.text 调用您的委托。

于 2013-08-18T03:42:37.743 回答