我是IOS的新手,
我一直在努力将数据从子视图传递到父视图。我已将父表视图定义为静态,四个单元格连接到其他表视图。这些表视图具有我想要的数据,当我选择将其传递给我的静态单元格时。我阅读了许多关于传递数据的解决方案,使用了委托和segue,但似乎没有一个对我有用。即,静态表视图中的重复单元格有两个标签和UILabel
重复,我不希望它改变和重复详细说明这是一个当触发披露指示器并且新表格视图与数据一起呈现时选择能够单击后退按钮以在我的repeatDetail标签中包含选择的数据。我的静态表使用故事板嵌入导航控制器中。我想在FirstChildViewController
中选择数据来修改所选数据,即RootViewController中的星期一到星期一。然而,在我的代码中选择儿童选中标记后的数据是那么快
当我回到RootVC时,什么都没有显示出来,当我回到Child时,没有任何选择也是如此。
1.将所选数据保存在子项中,仅在有新选择时更改
2.发送到RootVC时使用短周名称
3. repeatDetail获取所选数据
没有写太多,让我展示我做了什么。
in RootViewController.h // RootViewController is static
#import "FirstChildViewController"
@interface RootViewController: UITableViewController <repeatProtocol> //RootViewController COnfirms to the delegate
@property repeat, repeatDetail;
@end
next on my RootViewController.m
@implementaion RootViewController
@sysnthesis repeat,repeatDetail;
- (void) viewDidload
{
repeat.text = @"Repeat"
repeatDetail= //not show how call this label from 1stViewController
}
-(void) selectedValue:(NSString *)string //protocol method
{
FirstChildViewController *RVC =[[FirstChildViewController alloc] init];
RVC.delegate =self;
[self selectedValue:string]; //This part confuses me, i know i have to implement the delegate method but not sure if i implement it correctly.
}
-(void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
in FirstChildViewController.h
@class FirstChildViewController
@protocol repeatProtocol <NSObject>
@required
-(void) selectedValue:(NSString *)string;
@end
@interface FirstChildViewController: UITableViewController
{
NSArray *tableData;
id <repeatProtocol > repeatDelegate;
NSString *selectedDay;
}
@property (retain) id <repeatProtocol> repeatDelegate;
in FirstChildViewController.m
@synthesize tableData;
@synthesize repeatDelegate;
- (void) viewDidLoad
{
[super viewDidLoad]
tableData= [NSArray alloc] initWithArrays:@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday";
}
- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 7;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RepeatCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RepeatCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row]//strings from an array here;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = {tableView cellForRowAtIndexPath:indexPath];
cell.accessaryType = UITableViewCellAccessaryCheckMark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.delegate respondsToSelector:@selector(selectedValue:)])
{
[self.myDelegate selectedValue:selectedDay];
NSLog(@"string passed");
}
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"FirstChildViewController dismissed");
}
@end
答案 0 :(得分:0)
要弄清楚你的描述中发生的事情有点困难;所以我会重申我认为的问题。
您有一个UITableView
,其中显示的设置类似于您要在一系列远端视图控制器中修改的设置。但是你不确定在将数据返回到静态表视图时使用什么机制。基本上,您希望在远端控制器完成时捕获该数据。我不打算如何在根视图控制器中显示它,因为从代码示例中不清楚它。
尽管如此,我还是赞成不使用正式的委托协议。这只是一个回归的基准 - 所以协议似乎是浪费的形式。我会使用完成块。
所以你的FirstViewController
界面看起来像
typedef void(^WeekdayCompletionBlock)(NSString *dayName);
@interface FirstViewController : UIViewController
@property (nonatomic, strong) WeekdayCompletionBlock completionBlock;
@end
当您实例化FirstViewController
时,只需为其提供完成块。由于我认为您正在使用Storyboard,因此您需要使用prepareForSegue:
的{{1}}方法执行此操作。
RootViewController
最后,当用户将控制权交还给- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UIViewController *destinationController = segue.destinationViewController;
if( [destinationController isKindOfClass:[FirstViewController class]] ) {
[(FirstViewController *)destinationController setCompletionBlock:^(NSString *returnString){
// do something here with your string
// maybe you must reload your table
// it depends on where your returning data needs to display
}];
}
}
时,您需要执行该块。例如,是否有保存按钮等?你只需执行完成块,例如RootViewController
答案 1 :(得分:0)
或者,您可以在rootVC.h中创建全局NSString:
NSString *returnString;
如果你还没有在firstVC.h中包含rootVC.h。这允许从firstVC.m访问returnString:
#import "rootVC.h"
您可以在firstVC.m中分配returnString:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
returnString = <selected value>;
}
在NSBum指出的情况下,在rootVC.m中显示returnString:
-(void) viewDidload
{
repeat.text = @"Repeat";
repeatDetail.text = returnString;
}