2009-03-13 17 views
6

Tengo una clase simple "modelo" como tal (completa con el constructor por supuesto)cómo inicializar clase personalizada/objeto dentro del controlador usando Objective-C en el Iphone

@implementation Widget 
@synthesize name; 
@synthesize color; 

- (id) init 
{ 
    if (self = [super init]) 
    { 
     self.name = @"Default Name"; 
     self.color = @"brown"; 
    } 
    return self; 
} 

@end 

he declarado que como un elemento interno al controlador de este modo:

#import <UIKit/UIKit.h> 
#import "Widget.h" 

@interface testerViewController : UIViewController { 
    IBOutlet UITextField *stuffField; 
    Widget *widget; 
} 

@property (nonatomic, retain) UITextField *stuffField; 
@property (nonatomic, retain) Widget *widget; 
- (IBAction)buttonPressed:(id)sender; 
@end 

y ... estoy tratando de inicializarlo dentro de mi controlador de este modo:

#import "testerViewController.h" 
@implementation testerViewController 
@synthesize stuffField; 
@synthesize widget; 

- (IBAction)buttonPressed:(id)sender 
{ 
    stuffField.text = widget.name; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     widget = [[Widget alloc] init]; 
    } 
    return self; 
} 

pero ... no parece estar inicializando mi objeto porque mi campo de texto aparece en blanco todo el tiempo. ¿Alguna pista?

+0

Agregue un punto de interrupción en el botón Presionado: y asegúrese de que realmente se está llamando. Mientras estás allí, también puedes colocar el cursor sobre widget.name e inspeccionar su valor. – freespace

+0

Estoy demasiado acostumbrado al depurador de Visual Studio :(... ¿soy yo o son los valores en hexadecimal cuando recorro las propiedades en el depurador? – DaveJustDave

Respuesta

4

Intente utilizar

- método (void) {} ​​viewDidLoad a initiliaze sus datos

en su clase de interfaz de uso @class Widget en lugar de import "Widget.h"

y en su clase de implementación utiliza #importar "Widget.h"

y asegúrese de ingresar en su botón ¡Controlador presionado!

Cuestiones relacionadas