开发者

Scope of an instance Variable in Objective C

The question might sound pretty naive, ut this is really troubling me. I am trying to set an instance variable by calling an instance method method of a ViewController from another view controller. Basically here are the steps

  1. I am in ViewController1
  2. Initialized an object of ViewController2
  3. Called an instance variable to set some values to the instance variable of ViewController2
  4. Then finally called the presentModalViewController to load the view controller
  5. Using the variables in viewWillAppear method, But the app crashes and on debugging it shows BAD_EXEC

I have tried printing the same in 开发者_开发百科instance method and it prints there but crashing when trying to use somewhere outside the method.

I have also defined the property and also ynthesized the variable.

The only problem I can figure out is I am initializing the variable in methos ...Does that limit the scope of the variable.

Any kind of help would be highly appreciated.

Thanks in advance!!


Most likely you're running into a memory management error, and without code all I can advise is to make sure you are familiar with Cocoa's memory management system:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html


If you are creating the properties for the instance variable and synthesizing the getters and setters, you should be able to set the instance variable using dot notation:

viewController2.variable = foo;

or by using the setter method:

[viewController2 setVariable:foo];

You should not be trying to access the instance variables directly. By default, the scope is set to Protected, meaning that you can only access it by methods in the class, it's subclasses, and in category extensions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜