大佬教程收集整理的这篇文章主要介绍了nsmutablearray – 从先前视图控制器中的数组获取数据,将其放入另一个TableViewController中,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
SaveViewController.h:
#import <UIKit/UIKit.h> #import "RecordsViewController.h" //Next ViewController @interface SaveViewController : UITableViewController <UITableViewDatasource,UITableViewDelegate> { __weak IBOutlet UITextField *runnameField; RecordsViewController *RecordsVCData; } @property (weak,nonatomiC) IBOutlet UITextField *runnameField; @property (weak,nonatomiC) IBOutlet UITextView *runnotesTextView; @property (weak,nonatomiC) IBOutlet UIBarButtonItem *saveBarItem; @property (nonatomic,retain) RecordsViewController *RecordsVCData; - (IBACtion)saverun:(UIBarButtonItem *)sender; - (IBACtion)goAwayKeyboard:(id)sender; @end
SaveViewController.m:
- (IBACtion)saverun:(UIBarButtonItem *)sender { RecordsViewController *RecordsVC = [[RecordsViewController alloc] initWithNibName:nil bundle:nil]; RecordsVC.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; self.RecordsVCData = RecordsVc; [RecordsVC.runsArray addObject:[NSString StringWithFormat:@"%@",runnameField.text]]; [self presentModalViewController:recordsVC animated:YES]; } - (IBACtion)goAwayKeyboard:(id)sender { [sender resignFirstResponder]; }
RecordsViewController.h:
#import <UIKit/UIKit.h> @interface RecordsViewController : UITableViewController <UITableViewDatasource,UITableViewDelegate> { NSMutableArray *runsArray; } @property (weak,nonatomiC) NSString *runname; @property (strong,nonatomiC) NSString *runnotes; @property (weak,nonatomiC) UITableViewCell *cell; @property (retain,nonatomiC) NSMutableArray *runsArray; @end
RecordsViewController.m:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath: (NSIndexPath *)indexPath { // Identifier for retrieving reusable cells. static NSString *cellIdentifier = @"MyCellIdentifier"; // Attempt to request the reusable cell. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reusEIDentifier:cellIdentifier]; // No cell available - create one if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reusEIDentifier:cellIdentifier]; [cell.textLabel setText:[NSString StringWithFormat:@"%@",[runsArray objectATindex:indexPath.row]]]; } // Set the text of the cell to the runnameString. [cell.textLabel setText:[NSString StringWithFormat:@"%@",[runsArray objectATindex:indexPath.row]]]; return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [runsArray count]; }
任何帮助都很受欢迎.
谢谢,
Dhruv直升机
在RecordsViewController中,您将在init方法中注册通知:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundLeorNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundLeorNil]; if (self) { [[NsnotificationCenter defaultCenter] addObserver:self SELEctor:@SELEctor(dataLoaded:) name:@"runsArrayLoaded" object:nil]; // Custom initialization } return self; }
同样在RecordsViewController中,您将需要在收到通知时调用的方法.在这种情况下,它是dataLoaded:
-(void) dataLoaded:(Nsnotification *)notif { if (notif.userInfo){ runsArray = [notif.userInfo objectForKey:@"newdata"]; [self.tableview reloadData]; } }
使所有这些神奇工作的部分在SaveViewController类中的saverun方法中.
- (IBACtion)saverun:(UIBarButtonItem *)sender { RecordsViewController *RecordsVC = [[RecordsViewController alloc] initWithNibName:nil bundle:nil]; RecordsVC.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; [runsArray addObject:[NSString StringWithFormat:@"%@",runnameField.text]]; [self presentModalViewController:recordsVC animated:YES]; // notify the RecordsViewController that new data is available NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] initWithCapacity:1]; [userInfo setObject:runsArray forKey:@"newdata"]; [[NsnotificationCenter defaultCenter] postNotificationName:@runsArrayLoaded" object:nil userInfo:(NSDictionary*)userInfo]; }
最后,您必须清理并删除RecordsViewController类中的通知观察.在viewWillDisappear中,添加以下行:
[[Nsnotifcation defaultCenter] removeObserver:self name:@"runsArrayLoaded" object:nil];
以上是大佬教程为你收集整理的nsmutablearray – 从先前视图控制器中的数组获取数据,将其放入另一个TableViewController中全部内容,希望文章能够帮你解决nsmutablearray – 从先前视图控制器中的数组获取数据,将其放入另一个TableViewController中所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。