开发者

Unable to get result from my data class methods when access from ViewController

I've created a data class name Person with an integer and a bool instance variables. Inside my ViewController "initWithNibName" I initialize

    anObject = [[Person alloc] init];

Inside the View controller have defined an IBAction. I want to toggle the bool value in person class on each click. I can display text on each click and even append it. But unable to print the int value or bool value from my Data Class.

Person.h file

   @interface Person : NSObject {

       int pheight;
       BOOL palive;


   }

   -(Person*) init;
   -(void) setPersonheight: (int) h andAlive: (BOOL) pn;
   -(int) personHeight;
   -(void) display;
   -(BOOL) isPersonAlive;

   @end

Person.m file

   #import "Person.h"


   @implementation Person

   -(Person*) init{

       self = [super init];

    if (self){

        [self setPersonheight:5 andAlive:YES];
    }       

    return self;
   }

   -(void) setPersonheight: (int) h andAlive: (BOOL) pn{

       pheight = h;
    palive = pn;

   }

   -(int) personHeight{

    return pheight;

   }


   -(void) display{

    if(开发者_运维知识库palive){
        palive = NO; 
    }
    else {
        palive = YES;
    }
   }

   -(BOOL) isPersonAlive{

    return YES;

   }

   @end

*ViewController.h file

   @interface Demo9ViewController : UIViewController {

    UILabel *outlet;
    Person *anObject;
    //id anObject;

   }

   @property (nonatomic, retain) IBOutlet UILabel *outlet;
   -(IBAction) displayPerson:(id) sender;

   @end

*View controller.m file

   -(IBAction) displayPerson:(id) sender{

    [anObject display];
    NSString *myPerson = [NSString stringWithFormat:@"%d",[anObject personHeight]];
    NSString *string = [outlet.text stringByAppendingString:myPerson];
    outlet.text = string;
   }

Can someone tell, what wrong am I doing here ?

Thanks,

Regards, M Tahir


Have you correctly hooked up outlet to the UILabel?

There are quite a few other problems here with your naming, not using properties, but first make sure you outlets and actions are set up correctly.

What is in outlet.text?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜