2012-03-15 33 views
17

Tôi đang sử dụng kịch bản cho một ứng dụng iOS, kịch bản của tôi trông như thế này: http://d.pr/7yAY (droplr url)selector không được nhận gửi đến dụ sử dụng Storyboards

Vấn đề là khi tôi nhấp vào nút đăng nhập, tôi gửi tên người dùng bắt vào bộ điều khiển xem bảng sự kiện. Để làm điều đó tôi sử dụng chức năng prepareForSegue, nhưng rõ ràng khi tôi cố gắng thiết lập tên người dùng trong ném một ngoại lệ.

Mã của tôi là như sau:

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

- (IBAction) logintButton:(id)sender; 
@property (weak, nonatomic) IBOutlet UITextField *username_tf; // textfield 

@end 

ViewController.m

#import "ViewController.h" 
#import "EventsTableViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize username_tf; 


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([[segue identifier] isEqualToString:@"ToMainApp"]) 
    { 
     EventsTableViewController * dest = (EventsTableViewController *)[segue destinationViewController]; 
     NSString * username = [[NSString alloc] initWithFormat:@"%@", username_tf.text]; 
     [dest setUsername:username]; 
    } 
} 


- (IBAction) logintButton:(id)sender 
{ 
    //NSLog(@"Logint button pressed"); 
    [self performSegueWithIdentifier:@"ToMainApp" sender:sender]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [self setUsername_tf:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

EventsTableViewController.h

#import <UIKit/UIKit.h> 

@interface EventsTableViewController : UITableViewController 
{ 
    NSString * username; 
} 

@property (nonatomic, retain) NSString * username; 

@end 

EventsTableViewController.m

#import "EventsTableViewController.h" 

@interface EventsTableViewController() 
@end 

@implementation EventsTableViewController 

@synthesize username; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

... 

@end 

Trường hợp ngoại lệ ném là:

2012-03-15 14:19:27.304 StoryboardAssistance[30989:f803] 
-[UINavigationController setUsername:]: unrecognized selector sent to instance 0x68abf60 2012-03-15 14:19:27.306 
StoryboardAssistance[30989:f803] *** Terminating app due to uncaught 
exception 'NSInvalidArgumentException', reason: 
'-[UINavigationController setUsername:]: unrecognized selector sent to 
instance 0x68abf60' 
*** First throw call stack: (0x13c9022 0x155acd6 0x13cacbd 0x132fed0 0x132fcb2 0x28e6 0x43e4be 0xdb5ab 0x2974 0x13cae99 0x1614e 0x160e6 
0xbcade 0xbcfa7 0xbc266 0x3b3c0 0x3b5e6 0x21dc4 0x15634 0x12b3ef5 
0x139d195 0x1301ff2 0x13008da 0x12ffd84 0x12ffc9b 0x12b27d8 0x12b288a 
0x13626 0x253d 0x24a5) terminate called throwing an exception(lldb) 

Bất kỳ lời đề nghị?

+0

Bạn có thể thực hiện gỡ rối từng bước và tìm ra dòng nào đang thực sự gặp sự cố không? Ngoài ra 'NSString * username = [[NSString alloc] initWithFormat: @"% @ ", username_tf.text];' chỉ có thể là 'NSString * username = username_tf.text'. – Gobot

+0

Bạn có thể cập nhật hình ảnh của bảng phân cảnh của mình không? Nó hiện đang 404. Cảm ơn – cwiggo

Trả lời

34

Bộ điều khiển chế độ xem đích của segue là Bộ điều khiển điều hướng của bạn, không phải bộ điều khiển Chế độ xem bảng sự kiện.

Bạn có thể lấy bộ điều khiển Sự kiện bằng cách truy cập thuộc tính topViewController của Bộ điều hướng.

Hãy thử điều này:

UINavigationController *navController = (UINavigationController*)[segue destinationViewController]; 
EventsTableViewController *eventsController = [navController topViewController]; 

NSString * username = [[NSString alloc] initWithFormat:@"%@", username_tf.text]; 
[eventsController setUsername:username]; 
+1

Đó là nó, cảm ơn – JohnnyAce

+0

Đây chính xác là những gì tôi đang tìm kiếm! – josh123a123

16

Ngoài ra, hãy thử này: -

(Nó giống như câu trả lời jonkroll nhưng trong một dòng và loại bỏ các cảnh báo "kiểu con trỏ không tương thích khởi 'ViewController * __ mạnh mẽ' với biểu thức thuộc loại UIViewController * "

NameOfViewController *vc = (NameOfViewController *)[[segue destinationViewController] topViewController]; 
+1

Giải pháp một dòng tuyệt vời. Đã làm cho tôi! – Groot

4

Một điều nữa cần xác minh là bạn đã chỉ định đúng ViewCo đích của mình ntroller là lớp thích hợp trong Interface Builder. Theo mặc định, điều này sẽ là UIViewController hoặc UITableViewController v.v. Nếu bạn có một lớp tùy chỉnh với getters/setters, bạn cần phải nhớ thay đổi lớp trong Interface Builder để phản ánh tương ứng, nếu không bạn sẽ nhận được thông báo lỗi chọn không hợp lệ.

+0

Có +1, cảm ơn. – PeterPurple

+0

Tôi cảm thấy ngu ngốc vì tôi quên làm điều này, cảm ơn vì đã nhắc nhở. –

Các vấn đề liên quan