0

I am trying to get a URL from my user in a TextField and then process the result. Presently an exception is being thrown with the following -[UITextField length]: unrecognized selector sent to instance in the NSURLRequest below when I supply 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

我认为您的错误是,您将 UITextField 传递给期望 NSString 的方法。改变

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 回答