user1911
user1911

Reputation: 710

Picker to NSString to UITextField

I have this:

AddSightingViewController.h

#import <UIKit/UIKit.h>

@class MovieSighting;

@interface AddSightingViewController : UIViewController <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *movieTitleInput;
@property (weak, nonatomic) IBOutlet UITextField *directorInput;
@property (weak, nonatomic) IBOutlet UITextField *estrenoInput;
@property (strong, nonatomic) MovieSighting *movieSighting;

@end

and this:

AddSightingViewController.m

#import "AddSightingViewController.h"

#import "MovieSighting.h"

@interface AddSightingViewController ()

@property (strong, nonatomic) NSArray *array;
@property (strong, nonatomic) UITextField *genero;

@end

@implementation AddSightingViewController

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ((textField == self.movieTitleInput) || (textField == self.estrenoInput) || (textField == self.directorInput)) {
        [textField resignFirstResponder];
    }
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *generos = [[NSArray alloc] initWithObjects:@"Drama",@"Fantástico",@"Aventuras",@"Policíaco",@"Romántica",@"Comedia",@"Documental",@"Terror", nil];

    self.array = generos;
}

#pragma mark Picker Data Source Methods


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [_array count];
}

#pragma mark Picker Delegate Methods

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [_array objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSString *generoInput = [self.array objectAtIndex:row];   

}

//
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"ReturnInput"]) {
        if ([self.movieTitleInput.text length] || [self.directorInput.text length]|| [self.estrenoInput.text length] )
        {
            MovieSighting *sighting;

            NSUInteger *valor = (NSUInteger *)[[self.estrenoInput text] integerValue];

            sighting = [[MovieSighting alloc] initWithName:self.movieTitleInput.text anyo:valor genero: director:self.directorInput.text];
            self.movieSighting = sighting;
        }
    }
}

@end

How can I use:

NSString *generoInput = [self.array objectAtIndex:row];

To transform to a property UITextField that I created:

@property (strong, nonatomic) UITextField *genero;  

And then use that with the method sighting at the end? Then I can use it to compare on the method called textFieldShouldReturn.


I´ve been doing that:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSString *generoInput = [self.array objectAtIndex:row];
    UITextField *nuevo = (UITextField*)[generoInput capitalizedString];
    _genero = nuevo;

}

And prepareForSegue method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"ReturnInput"]) {
        if ([self.movieTitleInput.text length] || [self.directorInput.text length]|| [self.estrenoInput.text length]
            || [self.genero.text length])
        {
            MovieSighting *sighting;

            NSUInteger *valor = (NSUInteger *)[[self.estrenoInput text] integerValue];

            sighting = [[MovieSighting alloc] initWithName:self.movieTitleInput.text anyo:valor genero:self.genero.text director:self.directorInput.text];
            self.movieSighting = sighting;
        }
    }
}

But don´t work the app... Any idea? When I add other movie and clic on Add:

enter image description here Appeared that:

enter image description here Error code:

2013-12-19 21:43:19.875 MovieWatching[920:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AddSightingViewController 0x8a63190> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key staticDataSource.'

Upvotes: 0

Views: 357

Answers (1)

rmaddy
rmaddy

Reputation: 318814

You want:

self.genero.text = [generoInput capitalizedString];

Upvotes: 1

Related Questions