Tôi có một số FirstViewController
và SecondViewController
. Tôi đã tạo một nút trong số FirstViewController
để thực hiện phân đoạn theo phương thức SecondViewController
.Truyền dữ liệu trở lại với bỏ quaViewControllerAnimated
Trong SecondViewController
Tôi có tableView hiển thị danh sách 8 mục, khi tôi chọn một mục từ danh sách đó, I dismissViewControllerAnimated:
, quay lại FirstViewController
.
Điều tôi muốn làm là chuyển một chuỗi trở lại số FirstViewController
.
tôi đã sử dụng bài viết này như một tham khảo cho mã của tôi: dismissModalViewController AND pass data back
Vì vậy, đây là những gì tôi có:
trong FirstViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "SecondViewController.h"
@interface FirstViewController : UIViewController <SecondDelegate>
@end
trong FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController()
@end
@implementation FirstViewController
...
- (void)secondViewControllerDismissed:(NSString *)stringForFirst
{
NSString *theString = stringForFirst;
NSLog(@"String received at FirstVC: %@",theString);
}
@end
trong SecondViewController.h
#import <UIKit/UIKit.h>
@protocol SecondDelegate <NSObject>
-(void) secondViewControllerDismissed:(NSString *)stringForFirst;
@end
@interface SecondViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
__weak id myDelegate;
}
@property (nonatomic, weak) id<SecondDelegate> myDelegate;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@end
trong SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController()
@end
@implementation SecondViewController
@synthesize myDelegate;
@synthesize myTableView;
...
- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 8;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [atributoTableView dequeueReusableCellWithIdentifier:@"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
cell.textLabel.text = //strings from an array here;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:)])
{
[self.myDelegate secondViewControllerDismissed:@"SOME STRING HERE"];
NSLog(@"string passed");
}
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"SecondViewController dismissed");
}
@end
Khi tôi chạy ứng dụng Tôi có thể đi FirstViewController
-SecondViewController
, và khi tôi chọn một hàng từ tableView tôi quay trở lại FirstViewController
tốt. Vấn đề là chuỗi "MỘT SỐ STRING TẠI ĐÂY" đã không được trả lại.
Tôi đang thiếu gì?
Nhân tiện, tôi không chắc liệu điều này có liên quan hay không: Tôi đang sử dụng ARC và Bảng phân cảnh.
Bạn có đang đặt đại biểu không? Có phải nó đang đi đến dòng '[self.myDelegate secondViewControllerDismissed ...'? – iDev
Câu hỏi được định dạng rất độc đáo :) – Sid