PDA

View Full Version : selectTabViewItemAtIndex help




BollywooD
Apr 14, 2009, 01:22 AM
I am trying to remember window state in my app, and reload on launch. I save the tabViewItemAtIndex to defaults, and this works well. I cant seem however to read that integer back, and restore my tab?

Here is the function:

- (void) awakeFromNib
{
int tabState;
tabState = [[SCPreferences userDefaults] integerForKey:SCPreferencesTabStateKey];
if (tabState == 0)
{
[tabView selectTabViewItemAtIndex:0];
}
if (tabState == 1)
{
[tabView selectTabViewItemAtIndex:1];
}
if (tabState == 2)
{
[tabView selectTabViewItemAtIndex:2];
}
}

does anyone have any ideas?

I also would like to remember window size, somehow.



kainjow
Apr 14, 2009, 11:25 AM
Unless you have very good reason for using the SCPreferences APIs, don't. Use NSUserDefaults. Plus, isn't this code giving you an error? I don't see any class named SCPreferences.

Also your code could be reduced to this and avoid the if-else:
[tabView selectTabViewItemAtIndex:tabState];

To save a window's position and size, give it an Autosave name in IB.

Krevnik
Apr 14, 2009, 06:10 PM
Unless you have very good reason for using the SCPreferences APIs, don't. Use NSUserDefaults. Plus, isn't this code giving you an error? I don't see any class named SCPreferences.


Looks like he is using 'SC' as a prefix on his own code. This is actually a bad idea as the System Configuration APIs use 'SC' as their prefix. It will lead to confusion.

The one thing I'm confused about is why he would need his own object/class to expose the userDefaults. Subclassing NSUserDefaults usually doesn't provide much benefit either.

kainjow
Apr 14, 2009, 06:28 PM
Looks like he is using 'SC' as a prefix on his own code. This is actually a bad idea as the System Configuration APIs use 'SC' as their prefix. It will lead to confusion.

That's what I first thought, but System Configuration does have a SCPreferences API, but it serves a different purpose.

The one thing I'm confused about is why he would need his own object/class to expose the userDefaults. Subclassing NSUserDefaults usually doesn't provide much benefit either.

Yes, normally this wouldn't be needed, but if you're writing a plugin you might want to write a wrapper class since NSUserDefaults will use the main app's identifier instead of yours.

BollywooD
Apr 15, 2009, 01:58 AM
I am using SC as a prefix, for all my classes....
I didnt realise, it would cause confusions?

I am writing a plugin:
Safari Cookies (http://swwetpproductions.com/safaricookies/index.htm)

I think that the rembering tab state, mught be just bloat, and a little excessive, so Ill leave it out.


thanks for your help

kainjow
Apr 15, 2009, 07:59 AM
It's not bloat, it's just most likely a bug in your class since the code looks fine. You could also double-check and make sure your tab view is valid at this point too.