我在iPhone应用程序中有2个视图。 FirstViewController
和MultiSelectViewController
。
FirstViewController
中有一个按钮可转到MultiSelectViewController
。在MultiSelectViewController
我有一个tableviewcontroller
来多选并使用完成按钮将结果发送回FirstViewController
我的问题是完成按钮。我不知道如何将数据发送回FirstViewController
。它必须与dissmissviewcontroller
。
这是MultiSelectViewController
@protocol MultiSelectDelegate <NSObject>
-(void) multiselectViewControllerDismissed;
@end
@interface MultiSelectViewController : UITableViewController
{
__weak id myDelegate;
}
@property(nonatomic,retain)NSArray *myData;
@property(nonatomic, retain)NSMutableArray *selectedData;
@property (nonatomic, weak) id<MultiSelectDelegate> myDelegate;
这是我在MultiSelectViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.allowsMultipleSelection = YES;
selectedData=[[NSMutableArray alloc] init];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(multiselectViewControllerDismissed)];
self.navigationItem.rightBarButtonItem = barButton;
}
最后这是我完成的按钮操作:
-(void)multiselectViewControllerDismissed
{
NSLog(@"%@",selectedData);
}
我不明白如何发送数据并返回FirstViewController
答案 0 :(得分:3)
你重新定义
multiselectViewControllerDismissed
委托方法为
multiselectViewControllerDismissedWithData:(NSMutableArray *)dataSelected
并且,在FirstViewController
的.h文件中实现委托,即
@interface FirstViewController: UIViewController <MultiSelectDelegate>
并在FirstViewController.m
的按钮操作中将MultipleSelectViewController的委托指定为self。即,
MultipleSelectViewController * msvc = [[MultipleSelectViewController alloc] init];
msvc.myDelegate = self;
并实施
-(void)multiselectViewControllerDismissedWithData:(NSMutableArray *)dataSelected
FirstViewController.m
并且,在MultipleSelectViewController.m
的完成按钮操作方法中,使用委托调用方法multiselectViewControllerDismissedWithData
,即
[self.myDelegate multiselectViewControllerDismissedWithData:selectedData];
那就是它。
您现在可以将selectedData
数组从MultipleSelectViewController
传递到FirstViewController
答案 1 :(得分:0)
在obj-c中传递数据的两种标准方法:
LocalNotifications
的发布商订阅者模式。第一个视图控制器侦听特定位置通知,第二个视图控制器在被解除之前,使用数据进行广播。我建议您使用第一种方法。一些示例代码:
在.h文件中:
@interface FirstViewController:UIViewController
@property NSMutableArray *receivedData; //property to receive selected data
@end
在.m文件中:
MultiSelectViewController *msvc = [MultiSelectViewController alloc] init];
msvc.presentingViewController = self; // pass reference of 1st VC to 2nd VC
[self presentViewController:msvc animated:YES];
在MultiSelectViewController.h文件中:
#import "FirstViewController.h"
@interface MultiSelectViewController: UITableViewController
...
@property FirstViewController *presentingViewController;
...
@end
在MultiSelectViewController.m文件中:
-(void)multiselectViewControllerDismissed
{
NSLog(@"%@",selectedData);
presentingViewController.receivedData = selectedData;
}
答案 2 :(得分:0)
首先将您的委托方法创建为
-(void)dismiss:(NSString *)str;
while确实选择了从indexpath.row获取值 并将其存储为nsstring。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.valueString=[NSString stringWithFormat:@"%@",[self.arrayValue objectAtIndex:indexPath.row]];
NSLog(@"%d",indexPath.row);
}
完成按钮点击:
调用您的委托方法,如
[self. myDelegate dismiss:"your string which you get from table"]
[[self navigationController] popViewControllerAnimated:YES];
并在您的第一个视图控制器中......
导入视图控制器类并调用委托方法..
为第二个视图控制器创建实例...
MultiSelectViewController *txtNext=[[MultiSelectViewController alloc]init];
txtNext. myDelegate =self;
然后
-(void)dismiss:(NSString *)str
{
NSString *strng=str;
nslog("%@",strng);
}