1

I am creating a screen "X". Within this screen I'm creating a UITextField and after the change of "TextColor" and "Font" this respective UITextField. However, when I go to the screen "Y" and return to the screen "X" using a modal segue UITextField lose the settings full of "TextColor" and "Font". How can I solve this problem?

Thank you very much!

EditaCanalTVVC.m

BOOL firstTime;

@interface EditaCanalTVVC ()

@property (strong, nonatomic) IBOutlet UIView *viewNumCanal;
@property (strong, nonatomic) IBOutlet UITextField *txtNumCanal;
@property (strong, nonatomic) IBOutlet UILabel *lblNumCanal;
@property (strong, nonatomic) IBOutlet UIView *viewTudo;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *botaoCancelar;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *botaoOK;

- (IBAction)btnCancelar:(id)sender;
- (IBAction)btnOK:(id)sender;

@end

@implementation EditaCanalTVVC
{
    NSString *strNomeCanal;
    NSString *strNumCanal;
    NSString *strDelay;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

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

- (void)viewWillAppear:(BOOL)animated
{    
    [self updateScreen];
    [self.tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    firstTime = YES;

    [self.txtNumCanal setTextColor: [UIColor darkGrayColor]];
    [self.txtNumCanal setFont:[UIFont fontPrincipal:17]];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.tableView.alwaysBounceVertical = NO;

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    // Initialization code
    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackOpaque;
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithTitle:@"Cancelar" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"OK" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                           nil];
    [numberToolbar sizeToFit];
    self.txtNumCanal.inputAccessoryView = numberToolbar;

    UITapGestureRecognizer *tapGestureDimmer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toqueViewNumCanal:)];
    tapGestureDimmer.numberOfTapsRequired = 1;
    [self.viewNumCanal addGestureRecognizer:tapGestureDimmer];


}

- (void)viewDidAppear:(BOOL)animated
{
    [self updateScreen];
}

- (void)viewDidLayoutSubviews
{
    self.viewTudo.backgroundColor = [UIColor corFundoCinza];
    self.navigationController.navigationBar.barTintColor = [UIColor corFundoCinza];

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];

    [self updateScreen];
}

- (void)updateScreen
{    
    self.txtNumCanal.delegate = self;

    if (self.addCanal)
    {
        if (firstTime)
        {
            self.nome_canal = @"Selecione o canal";
            self.delay = @"Selecione o delay";
            self.num_canal = @"Digite o número";
            firstTime = NO;
        }
    }

    self.lblNumCanal.text = @"Número";
    self.lblNumCanal.textColor = [UIColor corLabelClaro];
    self.viewNumCanal.backgroundColor = [UIColor whiteColor];
    self.txtNumCanal.text = [NSString stringWithFormat:@"%@", self.num_canal];
    [self.txtNumCanal setTextColor: [UIColor darkGrayColor]];
    [self.txtNumCanal setFont:[UIFont fontPrincipal:17]];
}

#pragma mark - Toques nas imagens de edição

- (void)toqueViewNumCanal:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateRecognized)
    {
        [self.txtNumCanal becomeFirstResponder];
    }
}

#pragma mark - Eventos do TextField

-(void)cancelNumberPad
{
    [self.txtNumCanal resignFirstResponder];
    self.txtNumCanal.text = self.num_canal;
}

-(void)doneWithNumberPad
{
    [self.txtNumCanal resignFirstResponder];

    if ([self.txtNumCanal.text isEqualToString:@""])
        self.txtNumCanal.text = self.num_canal;
    else
        self.num_canal = self.txtNumCanal.text;

    [self updateScreen];
}

#pragma mark - Eventos da Tabela

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier1 = @"CellCanal";
    static NSString *CellIdentifier3 = @"CellDelay";

    UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    UITableViewCell *cell3 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];

    int isCellCustom = 0;

    if (!cell1)
        cell1 = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier1];

    if (!cell3)
        cell3 = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier3];

    switch (indexPath.row)
    {
        case 0:
        {
            isCellCustom = 1;
            cell1.textLabel.text = @"Canal";
            cell1.detailTextLabel.text = [NSString stringWithFormat:@"%@", self.nome_canal];
            cell1.detailTextLabel.textColor = [UIColor darkGrayColor];

        }   break;

        case 1:
        {
            isCellCustom = 2;
            cell3.textLabel.text = @"Delay";
            cell3.detailTextLabel.text = [NSString stringWithFormat:@"%@", self.delay];
            cell3.detailTextLabel.textColor = [UIColor darkGrayColor];

        }   break;
    }

    cell1.textLabel.textColor = [UIColor corLabelClaro];
    [cell1 setBackgroundColor:[UIColor whiteColor]];

    cell3.textLabel.textColor = [UIColor corLabelClaro];
    [cell3 setBackgroundColor:[UIColor whiteColor]];

    if (isCellCustom == 1)
        return cell1;
    else
        return cell3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row)
    {
        case 0:
        {
//            [self performSegueWithIdentifier:@"segueListaCanaisTV" sender:nil];

            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            ListaCanaisTVVC *lista = [storyboard instantiateViewControllerWithIdentifier:@"listaCanaisTV"];

            lista.tipoDaLista = 1;

            [self presentViewController:lista animated:YES completion:nil];

        } break;

        case 1:
        {


        } break;

        default:
            break;
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

#pragma mark - Botões

- (IBAction)btnCancelar:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btnOK:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIStoryBoardSegue

//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//{    
//    if ([segue.identifier isEqualToString:@"segueListaCanaisTV"])
//    {
//        ListaCanaisTVVC *list = (ListaCanaisTVVC *)segue.destinationViewController;
//        
//        list.tipoDaLista = 1;
//        
//    }    
//}

@end

EditaCanalTVVC.h

#import <UIKit/UIKit.h>

@interface EditaCanalTVVC : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

@property (nonatomic) int id_canal_local;
@property (nonatomic) int id_canal_tv;
@property (nonatomic) NSString *nome_canal;
@property (nonatomic) NSString *num_canal;
@property (nonatomic) NSString *delay;

@property (nonatomic) int id_dispositivo;

@property (nonatomic) BOOL addCanal;
@property (nonatomic) BOOL canalSelecionado;

- (void)updateScreen;

@end
  • How and where do you create this UITextField? – Rémi Belzanti Jan 06 '15 at 16:27
  • The UITextField is being created from a StoryBoard within a UIViewController – Helton Fernandes Sampaio Jan 06 '15 at 16:40
  • move the code to viewDidLoad. viewWillAppear is called every time the VC's main view appears, so that `updateScreen` is called after each segue to the VC, which is unnecessary since these settings only need to be set once. as far as the text, if you want to be 100% sure that the text will stay, reference it in an instance variable and keep it around until you need to change it – Louis Tur Jan 06 '15 at 17:02
  • Hello Louis Tur, unfortunately your solution did not work. Upon entering the "X" screen for the first time, the UITextField works perfectly, however after I call another screen "Y" via modal and then immediately return to the screen "X", the UITextField keep losing their initial settings. – Helton Fernandes Sampaio Jan 06 '15 at 17:40
  • the modal view is being dismissed how? do you have the conditional logic to hold that something like `self.textResponse == UITextField.text` and `UITextField.text = self.textResponse ? : @"Placeholder";` – Louis Tur Jan 06 '15 at 18:24
  • The screen "X" contains UITextField and a button that uses "[self performSegueWithIdentifier: @" segueList "sender: nil];" to call up the "Y" modal route. For now I am not worried about the value that the UITextField returns me, what worries me is that the UITextField loses configuration "TextColor" and "Font" after returning from screen "Y". – Helton Fernandes Sampaio Jan 06 '15 at 18:58
  • Well, i've just created a new project with just 2 viewControllers and it works perfectly for me in `viewDidLoad`. I present modally a new viewController using a triggered segue (via storyBoard) on a UIButton i made (via storyBoard). On the secondViewController there is a button that dismiss this viewController. All is OK. What is the iOS version you're working with ? – Rémi Belzanti Jan 07 '15 at 16:31
  • I'm working with iOS 8.1 and Xcode 6.1.1 You have changed the "TextColor" and the "Font" the UITextField? After you returned to the screen, the settings remained the same? – Helton Fernandes Sampaio Jan 08 '15 at 12:32
  • Yes, it works well for me, settings stay put. It seems i can't reproduce this bug. I'm working with iOS 8.1 and Xcode 6.1.1 too. Did you try to do it in a new Xcode project? Try so and note any difference between this and your actual code. – Rémi Belzanti Jan 09 '15 at 10:31
  • Ok, I'll try again, but still I could not solve this problem. Is there any possibility that this might be some setting of the previous screens that is affecting the UITexteField? – Helton Fernandes Sampaio Jan 09 '15 at 11:59

2 Answers2

0

Try to remove the self performSegue in your .m file and manage it via storyBoard instead.

To do so, select your button and in the Connection Inspector (the arrow in a circle) define your segue via the Triggered Segue part: drop the point on the Y viewController and define it as "Present modally" (as "Modal" is deprecated).

When you'll use the button, the Y viewController will show up, you don't need to code a single line for that.

I also tried to do it your way, with coded segue, but i can't reproduce the bug (event with sender:selfon performSegue).

Rémi Belzanti
  • 413
  • 5
  • 12
  • The problem is that I need to use `self performSegue`, because I have to send a variable as parameter. For this reason, I could not apply your solution. You know another way to solve this issue? I will be very grateful. – Helton Fernandes Sampaio Jan 08 '15 at 12:55
0

Ok, so if you need to send variables as parameter, why don't you try this:

- (IBAction)didTapButton:(UIButton *)sender{
    SecondView *secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
    secondView.someProperty = @"someValue";
    [self presentViewController:secondView animated:YES completion:nil];
}

Put that code in your first view and link this to the button you have. This way you can send whatever you want to your second class, and you avoid using performSegue since you have issues with it.

If you're using storyboard, do this to get the secondView:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SecondView *secondView = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];

Then to come back to the first view, put that code in the second view:

[self dismissViewControllerAnimated:YES completion:nil];
Rémi Belzanti
  • 413
  • 5
  • 12
  • Hello, actually works passing parameters by the method you mentioned, however the problem with the UITextField still remains the same. I do not know what to do. – Helton Fernandes Sampaio Jan 12 '15 at 15:31
  • Can you edit your question and copy/paste your entire code into it so i can take a look at it all? – Rémi Belzanti Jan 12 '15 at 15:43
  • Well, i can't see anything that would change your textfield properties in this view. Try to remove the UITextFieldDelegate as you don't use any delegate method for textFields. I also suggest that you start over from a simple storyboard (or couple of .xib) as i did. It will work fine. Then you copy/paste your code into it and test each step so you can identify where this issue comes from. Boa sorte! – Rémi Belzanti Jan 14 '15 at 09:29