0

TextFieldでユーザーからURLを取得し、結果を処理しようとしています。-[UITextField length]: unrecognized selector sent to instance現在、を指定すると、以下のNSURLRequestで次のような例外がスローされますURLWithString: (NSString *)self.urlNameInput]

myViewController.h

@class myViewController;
@interface myViewController : UIViewController <UITextFieldDelegate, NSURLConnectionDataDelegate, NSURLConnectionDelegate>
{
    NSMutableData *receivedData;
}
@property (weak, nonatomic) IBOutlet UITextField *urlNameInput;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end

myViewController.m

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.urlNameInput) {
        [textField resignFirstResponder];
        NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString: (NSString *)self.urlNameInput] cachePolicy:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:60.0];
        // create the connection with the request
        // and start loading the data

        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
        if (theConnection) {
            // Create the NSMutableData to hold the received data.
            // receivedData is an instance variable declared elsewhere.
            receivedData = [NSMutableData data] ;
        } else {
            // Inform the user that the connection failed.
            NSLog(@"Their is an error with that URL.");
        }

    }
    return YES;

}
4

1 に答える 1

3

あなたの間違いは、NSStringを期待するメソッドにUITextFieldを渡すことだと思います。変化する

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString: (NSString *)self.urlNameInput] cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlNameInput.text] cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];
于 2013-03-01T22:58:52.940 に答える