0

This is my first question and I am very new in programming field. I have a tab bar controller and I want to transfer data from FVC(1st view controller) to SVC(second View Controller). In FVC ,I am taking controllers contained in tab bar controller in an array (VCArray) and assigning the second object of that array to instance of SVC and setting properties of SVC with appropriate data of FVC but those properties appears nil in SVC.

and 1 more interesting thing is that when I check the SVC instance which was assigned as 2nd object of the VCArray with [isOFKindClass SVC] and [isOFKindClass FVC] both come true..How is it possible? An object can have two classes? and if I check [isOfKIndClass NSArray] it comes false..it means theres nothing wrong in implementation.

Sorry about my bad english..:p

4

1 回答 1

0

The answer to part1 is that you need to expose properties on SVC, so that another class can access them, and the FVC needs to import the interface file (the .h) of svc. Thus you almost always need two things to do this: a way to find the class you want to make changes to, and the interface of that class. The property will look like this:

SVC.h:
@property (nonatomic, strong) NSString *title;
- (void)doSomething;

FVC:
#import "SVC.h"

SVC *svc = ...; // get a reference to it
svc.title = @"Howdie!";
[svc doSomething]; // tell the class to use the title you just set, for example

To answer your second question, there are two types of these "is.." methods, isKindOfClass and isMemberOfClass. The first says is the current object a type of the provided class, or ANY superclass. The second only passes if the class is in fact an exact member. For example:

NSMutableData *data;
[data isKindOfClass:[NSData class]] == YES
[data isKindOfClass:[NSMutableData class]] == YES

[data isMemeberOfClass:[NSData class]] == NO
[data isMemeberOfClass:[NSMutableData class]] == YES

EDIT: So the data never makes it into SVC. Well try this - a property is just a shortcut to have an ivar, a getter, (and usually) a setter. You can actually provide your own setter. So you say that (using my example above), that in SVC title is always nil, even though its set by SVC. There are only three reasons this can happen:

  • FVC has a reference to another object, but in fact you called it SVC so when the value is set, its set to another class not SVC

  • SVC was a nil object when FVC set the value (ObjectiveC handles messages to nil just fine, so you will not see any errors on the console)

  • SVC has reset the value to nil unbeknownst to you in say viewWillAppear

So the way you can find this out is override the variable setter (again using my example):

- (void)setTitle:(NSString *)val
{
  title = val; // ARC way
  NSLog(@"SVC - just set title to %@", title);
}

Add this to SVC and see what happens.

于 2012-08-28T12:11:19.423 回答