UITextField丢失设置

时间:2015-01-06 16:22:48

标签: ios objective-c uitextfield uitextfielddelegate

我正在创建一个屏幕“X”。在这个屏幕中,我正在创建一个UITextField,在更改“TextColor”和“Font”之后,这个相应的UITextField。但是,当我进入屏幕“Y”并使用模态segue UITextField返回到屏幕“X”时,会丢失设置为“TextColor”和“Font”的设置。我该如何解决这个问题?

非常感谢!

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

2 个答案:

答案 0 :(得分:0)

尝试删除.m文件中的self performSegue,然后通过storyBoard进行管理。

要执行此操作,请选择您的按钮,然后在连接检查器(圆圈中的箭头)中通过Triggered Segue部分定义您的segue:删除Y viewController上的点并将其定义为&#34;以模态显示&#34 ; (&#34; Modal&#34;已弃用)。

当您使用该按钮时,Y viewController将显示,您不需要为此编码单行。

我也尝试按照您的方式使用编码segue,但我无法重现该错误(sender:selfperformSegue的事件。)

答案 1 :(得分:0)

好的,所以如果您需要将变量作为参数发送,为什么不试试这个:

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

将该代码放在第一个视图中,并将其链接到您拥有的按钮。 通过这种方式,您可以将您想要的任何内容发送到第二堂课,并且由于遇到问题而无法使用performSegue

如果您正在使用故事板,请执行此操作以获取第二个视图:

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

然后返回第一个视图,将该代码放在第二个视图中:

[self dismissViewControllerAnimated:YES completion:nil];