我创建了一个名为TAddAlarmController的控制器类,它有一个包含6行的tableview。当我点击第二行时,它导航到一个页面,这是一个新的控制器名称TAlarmNewController,它是一个tableviewcontroller,我在其中创建了一个nsmutablearray并用7个静态值填充该数组,所以当显示第二个控制器时,会显示tableview其中包含7个静态值。
我希望当我点击第二个控制器的任何一行时,特定行的单元格中存在的值应该设置为前一个控制器的detailtextlabel,即TAddAlarmController。
这是我的代码:
这是AddAlarmcontroller.h
#import <UIKit/UIKit.h>
@class StopSnoozeAppDelegate;
@class Alarm;
@class TAlarmNewController;
@interface TAddAlarmController : UITableViewController {
StopSnoozeAppDelegate *app;
IBOutlet UITableView *tblView;
NSDateFormatter *dateFormatter;
NSUndoManager *undoManager;
Alarm *am;
TAlarmNewController *anew;
}
@property(nonatomic,retain)NSDateFormatter *dateFormatter;
@property (nonatomic,retain)Alarm *am;
@property (nonatomic,retain)NSUndoManager *undoManager;
@end
这是我的.m文件
#import "TAddAlarmController.h"
#import "Alarm.h"
#import "TAlarmNewController.h"
@implementation TAddAlarmController
@synthesize dateFormatter;
@synthesize am;
@synthesize undoManager;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidUnload {
// Release any properties that are loaded in viewDidLoad or can be recreated lazily.
self.dateFormatter = nil;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
#pragma mark -
#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 6;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
/*
Dequeue or create and then configure a table cell for each attribute of the book.
*/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
//cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Time";
break;
case 1:
cell.textLabel.text = @"Repeat";
break;
case 2:
cell.textLabel.text = @"Sound";
break;
case 3:
cell.textLabel.text = @"Snooze Interval";
break;
case 4:
cell.textLabel.text = @"Alarm Message";
break;
case 5:
cell.textLabel.text = @"Snooze Penalty";
break;
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TAlarmNewController *controller = [[TAlarmNewController alloc]initWithNibName:@"TAlarmNewController" bundle:nil];
switch (indexPath.row) {
case 0:
controller.editedObject = @"Time";
break;
case 1:
[self.navigationController pushViewController:controller animated:YES];
[controller release];
default:
break;
}
}
- (NSDateFormatter *)dateFormatter {
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
}
return dateFormatter;
}
- (void)dealloc {
[super dealloc];
}
@end
这是TAlarmNewController.h
@class TAddAlarmController;
@interface TAlarmNewController : UITableViewController {
IBOutlet UITableView *tblView;
UIDatePicker *datePicker;
id editedObject;
TAddAlarmController *Addalarm;
NSMutableArray *days;//this is the array where i am storing 7 values statically
}
@property (nonatomic,retain) IBOutlet UITableView *tblView;
@property(nonatomic,retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, retain) id editedObject;
@property(nonatomic,retain)NSMutableArray *days;
@property (nonatomic, retain, readonly) TAddAlarmController *Addalarm;
-(IBAction)cancel;
-(IBAction)save;
@end
这是我的.m文件
#import "TAlarmNewController.h"
#import "TAddAlarmController.h"
@implementation TAlarmNewController
@synthesize editedObject,datePicker, tblView,days,Addalarm;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
UIBarButtonItem * saveButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
days =[[NSMutableArray alloc]initWithObjects:@"Every Monday",@"Every Tuesday",@"Every Wednesday",@"Every Thursday",@"Every Friday",@"Every Saturday",@"Every Sunday0",nil];
[super viewDidLoad];
}
- (TAddAlarmController *)Addalarm {
if (Addalarm == nil) {
Addalarm = [[TAddAlarmController alloc] initWithStyle:UITableViewStyleGrouped];
}
return Addalarm;
}
-(IBAction)save{
[self.navigationController popViewControllerAnimated:YES];
//
}
-(IBAction)cancel{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#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 [days count];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
cell.textLabel.text = [days objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)dealloc {
[datePicker release];
[super dealloc];
}
@end
答案 0 :(得分:0)
在你的FirstViewController中
1,保留一个名为detailTextValueFromSecondController的成员变量(NSString)。
2,创建一个名为
的函数-(void)refreshTableToSetDetailText:(NSString *)detailTextValue
然后在你的SecondViewController
中内部
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
放置以下代码:
for (int i =0; i < [[self.navigationController viewControllers] count]; i++)
{
UIViewController *aController = [[self.navigationController viewControllers] objectAtIndex:i];
if ([aController isKindOfClass:[FirstViewController class]])
{
FirstViewController *aFirstViewController = (FirstViewController *)aController;
[aFirstViewController refreshTableToSetDetailText:yourstringtosetondetaillabel];
[self.navigationController popToViewController:aController animated:YES];
}
}
答案 1 :(得分:0)
使用didSelectRowAtIndexPath
,找出用户选择的行,并将该值存储在NSUserDefaults
中,以获取特定的整数键,以便在视图之间进行通信。
NSUserDefaults *chosenrow = [NSUserDefaults standardUserDefaults];
[chosenrow setInteger:99 forKey: StringYouWantToDisplay];
在前一个控制器的cellForRowAtIndexPath
中,获取整数
NSInteger myInt = [chosenrow integerForKey:StringYouWantToDisplay];
并检查它是否为99.如果是这种情况,那么您可以确定选择了该特定值,并为单元格分配detailedTextLabel
。
要注意的事项:
确保reloadData
中的tableView为ViewWillAppear
,或者您无法看到detailedText
在第二个类的NSString
属性中设置所选行的值。确保在上一个视图中引用该类,以便您能够从NSString
中获取值。
NSUserDefaults
用于在两个视图之间进行通信。如果选择了行,请检查NSUserDefaults。如果未选择行,则无需分配详细的TextLabel。另一方面,如果选择了一行,则需要分配它..