开发者

Object Orientated Programming (HOW!)

开发者 https://www.devze.com 2023-03-31 01:27 出处:网络
I am new to object orientated programming i have two view controllers. a and b. in A i have a label called gradeLabel. I want to make it so I can display that text in view controller B. So i put this:

I am new to object orientated programming i have two view controllers. a and b. in A i have a label called gradeLabel. I want to make it so I can display that text in view controller B. So i put this:

-(NSString *)grade {
    return [gradeLabel text];
}

This is view controller b

#import "InfoInput.h"
@implementation illRememberViewController
  -(IBAction)previewTheInfo {
      InfoInput *info = [[InfoInput alloc开发者_运维问答] init];
      gradeText.text = info.grade;
  } 

But the label has no text/shows no text. How can I fix this. Am i using object orientated programming correctly?


First, you need to understand object oriented programming.

Once you do, then the answer would be "you need to either instantiate or grab an existing instance of the class that encapsulates grade and ask it for the grade".

And this line of code would work:

gradeText.text = thingThatHoldsTheGrade.grade;

No one is being mean; terse, maybe, but not intentionally mean. We've seen this question -- and ones very similar -- many, many, times. Ultimately, it boils down to a lack of knowledge of how the system is put together and how your app is integrated with the infrastructure provided by the system.

If you were trying to build a car, would your first question be "How do I connect a wire to the spark plug?" No. It would be "How does a car basically work, what are the various subsystems and how do they interact?" Then "if I want to make a custom car, how do I modify the 'basic car' template into whatever I want?"

Object Oriented Programming With Objective-C is good, but not really a basic tutorial as much as a guide.

The iOS Application Programming Guide is quite good, too.


When you do this

InfoInput *info = [[InfoInput alloc] init];

You are making a brand new instance, that is not the same instance where you put some text on the gradeLabel.

Assuming your might have pushed controllerB from ControllerA below code should pick the right instance of ControllerA.

gradeText.text = [((InfoInput*)self.parentViewController) grade];

No need to create new instance by [InfoInput alloc] init], remove this line.


Do you have an object of Class A around? You need to reference it somehow. Example:

A* a = [[A alloc] init]; 
gradeText.text=a.grade;

But it sounds like your first class is acutually a view controller. In that case, you don't store any info in there at all. You would normally introduce a thrid class called the model, and would just call on that to give youthe grade. Read up on the MVC pattern, please.
[Edit] I would store the grade information in a property in your UIApplicationDelegeate and read from there - it will be much easier for you than what you're doing right now. UIViewController are not normal objects. Normally, an Object represents a certain Thing (say, Object TestFForStudentA of Type TestResult) and can be used as such. However, UIViewController are linked to the UI they control (hence their name) and stays active while you're using the UI - but it's difficult to keep track of when you're switching views etc.

So, i would suggest:
YourAppDelegate.h:

@property (retain) NSString* grade

YourAppDelegate.m:

@synthesize grade;

ViewController A, save the value of grade to the app delegate once we leave the view

- (void) viewWillDisapper {
   YourAppDelegate* appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
   appDelegate.grade = gradeLabel.text;
}

And finally, you can show the grade in ViewController B:

-(IBAction)previewTheInfo {
  YourAppDelegate* appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
  gradeText.text = appDelegate.grade;
} 

Please note that this is just to give you a first idea. What you really want, is to create an object that stores the Grade and carry that object around between the view controllers

0

精彩评论

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