ios - 如何使从 View Controller 接收的字符串显示在另一个 View Controller 的 TableView 中

标签 ios objective-c uitableview

我正在制作一个具有两个 View Controller 的提醒应用。

第一个 View Controller 是显示提醒列表。

第二个 View Controller 有一个文本字段和一个用于添加提醒的添加按钮。

我需要将数据从第二个 View Controller 传递到第一个 View Controller ,以数组形式显示在 TableView 中。然后用户将能够检查和删除提醒。

第一位风投:

#import "mainHwTableViewController.h"
#import "ToDoItem.h"
#import "mainHwEditFormViewController.h"

@interface mainHwTableViewController ()
@end

@implementation mainHwTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //Data load (local)
    NSMutableArray *toDoItems = [[NSMutableArray alloc] init];
    [toDoItems addObject:self.];
    [self loadInitialData];

    //NavBar appearance
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]];
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue-Light" size:16.0], NSFontAttributeName, nil]];



    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadInitialData {

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.toDoItems count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];

    ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;

    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
    tappedItem.completed = !tappedItem.completed;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

/*
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
    mainHwEditFormViewController *source = [segue sourceViewController];
    ToDoItem *item = source.toDoItem;
    if (item != nil) {
        [self.toDoItems addObject:item];
        [self.tableView reloadData];
    }
}
*/

@end


    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    /*

    #pragma mark - Navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if (sender != self.addItemBtn) {
            [self dismissViewControllerAnimated:YES completion:nil];
        };
        if (self.addItemInput.text.length > 0) {
            self.toDoItem = [[ToDoItem alloc] init];
            self.toDoItem.itemName = self.addItemInput.text;
            self.toDoItem.completed = NO;
        }
    }
     */

    - (IBAction)unwindSegue:(id)sender {
        //[self dismissViewControllerAnimated:YES completion:nil];
    }

    - (IBAction)dismissKeyboard:(id)sender {
        [self resignFirstResponder];
    }

    - (IBAction)addUnwindSegue:(id)sender {

        NSString *toDoItem = self.addItemInput.text;

        [self dismissViewControllerAnimated:YES completion:nil];
    }
    @end

第二个风投:

#import "mainHwTableViewController.h"
#import "ToDoItem.h"
#import "mainHwEditFormViewController.h"

@interface mainHwTableViewController ()
@end

@implementation mainHwTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //Data load (local)
    NSMutableArray *toDoItems = [[NSMutableArray alloc] init];
    [toDoItems addObject:self.];
    [self loadInitialData];

    //NavBar appearance
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]];
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue-Light" size:16.0], NSFontAttributeName, nil]];



    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadInitialData {

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.toDoItems count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];

    ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;

    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
    tappedItem.completed = !tappedItem.completed;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

/*
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
    mainHwEditFormViewController *source = [segue sourceViewController];
    ToDoItem *item = source.toDoItem;
    if (item != nil) {
        [self.toDoItems addObject:item];
        [self.tableView reloadData];
    }
}
*/

@end

最佳答案

根据您提供的信息,我了解到您需要将信息从一个 View Controller 传递到另一个 View Controller ,因此为此您需要使用属性。

// First View Controller -- FirstViewController.h

@interface FirstViewController : UIViewController

@property (strong,nonatomic) NSString *stringToBePassed;


// First View Controller.m file
-(void)viewDidLoad{
 self.stringToBePassed=@"Hello World !";}


// SecondViewController.m

 -(void)viewDidLoad{
   //---Your code here----
   FirstViewController *ctr=[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
 ctr.stringToBePassed=@"New Value";
 [self.navigationController pushViewController:ctr animated:YES] ;

现在 stringToBePassed 变量中的值将是“新值”。

关于ios - 如何使从 View Controller 接收的字符串显示在另一个 View Controller 的 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903963/

相关文章:

ios - 在UnityPro4.6.0f3、Xcode5.0.2(5A3005)和IOS 8.1.1中构建TutorialHelloWorld时出现链接错误

iphone - 将 NSMutableArray 的随机 UIButtons 标题放到 NSMutablearray 的 UIButtons

ios - “无法识别的选择器已发送到实例”-添加搜索栏时出错

objective-c - iOS 上的矢量艺术

iphone - SIGABRT 同时将 NSDictionary 传递给 UITableView

ios - UITableViewAutomaticDimension

ios - 如何更改参数以接受 swift 3 中的变量?

ios - Metal API 验证崩溃

iphone - 如何在 iPhone 中创建网格 TableView

ios - UITests 在组中运行时失败但在独立运行时成功