UISwitch returns NULL? - objective-c

I have a UISwitch that returns (null) for some reason. Below is my code:
AddAlbumViewController:
// .h
IBOutlet UISwitch *photostreamSwitch;
#property (nonatomic, retain) IBOutlet UISwitch *photostreamSwitch;
// .m
#synthesize photostreamSwitch;
photostreamSwitch = [[UISwitch alloc] init];
NSLog(#"photostreamSwitch: %#", photostreamSwitch); // returns a not-null value
SecondViewController:
//.m
- (IBAction)createAlbum:(id)sender {
AddAlbumViewController *addAlbumViewController = [[AddAlbumViewController alloc] initWithNibName:#"AddAlbum" bundle:[NSBundle mainBundle]];
NSLog(#"Test Switch: %#",addAlbumViewController.photostreamSwitch); // returns null
[addAlbumViewController release];
}
I think I have everything set up right. If this helps, AddAlbumViewController is inside a UINavigationController and SecondViewController contains the UINavigationController.

The view controller is created but its view (i.e. its nib) is not loaded yet, therefore the property isn't connected yet. You can force the nib to load by accessing the view member of the controller:
- (IBAction)createAlbum:(id)sender {
AddAlbumViewController *addAlbumViewController = [[AddAlbumViewController alloc] initWithNibName:#"AddAlbum" bundle:[NSBundle mainBundle]];
UIView* tempView = addAlbumViewController.view;
NSLog(#"Test Switch: %#",addAlbumViewController.photostreamSwitch); // no longer null
[addAlbumViewController release];
}

Related

How to pass label data to another label in the second view controller

I've been searching for passing label data to another view controller, and found the following solution. However, it doesn't show the label text in another view controller.
please help
SecondViewController Header
#interface SecondViewController : UIViewController
{
IBOutlet UILabel *copy;
}
#property (nonatomic, retain) NSString *data;
In SecondViewController Implementation
copy.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleBottomMargin;
copy.textAlignment = NSTextAlignmentCenter;
copy.text = [NSString stringWithFormat:#"Your data: %#", _data]; // Make use of the exposed data property
[self.view addSubview:copy];
In FirstViewController Implementation
- (void)passDataForward
{
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.data = _label.text; // Set the exposed property
[self.navigationController pushViewController:secondViewController animated:YES];
}
1. Save the strings content in the first implementation file and load it in the second
Save your label.text in first class:
[[NSUserDefaults standardUserDefaults] setObject:Label.text forKey:#"Your key"];
Load it in the second class:
Label.text = [[NSUserDefaults standardUserDefaults] objectForKey:#"Your key"];
2. Use parameters
In the first class you have to implement the second
#import "SecondClassName.h"
In second class create a function with parameters.
- (void)setLabelText:(NSString *)LabelText
{
Label.text = LabelText
}
And in the first class you pass the data by adding these two lines:
SecondViewControllerName *Second = [[SecondViewControllerName alloc] init];
[Second setLabelText:label.text];
Its better to pass NSString then NSUserDefaults. Just to pass data it will be better if you use NSString
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.Stringdata = _label.text; // Set the exposed property
[self.navigationController pushViewController:secondViewController animated:YES];
In SecondViewController
#interface SecondViewController :UIViewController
{
}
#property (nonatomic, retain) NSString* name;
In the place to set text in label you can use
Label.text = Stringdata

calling custom delegates not working

I have made a custom delegate:
Example.h:
#protocol DismissExamplePopoverDelegate
- (void) dismissExamplePopover;
- (int) getUserID;
#end
#interface Example : UIViewController{
id<DismissExamplePopoverDelegate> delegate;
}
#property (nonatomic, assign) id<DismissExamplePopoverDelegate> delegate;
It is called in Example.m like follows:
[[self delegate] getUserID];
In my maincontroller.h:
#import "Example.h"
#interface MainScreen : UIViewController<DismissExamplePopoverDelegate>
maincontroller.m:
-(int) getUserID
{
return 100;
}
the view Example is called by the following method:
ExampleController = [[Example alloc] initWithNibName:#"Example" bundle:nil];
ExamplePopoverController = [[UIPopoverController alloc] initWithContentViewController:ExampleController];
[ExampleController setDelegate:self];
ExamplePopoverController.popoverContentSize = CGSizeMake(600, 480);
if ([ExamplePopoverController isPopoverVisible]) {
[ExamplePopoverController dismissPopoverAnimated:YES];
} else {
CGRect popRect = CGRectMake((self.EditExampleSelectAddButtonProperty.frame.origin.x),
(self.EditExampleSelectAddButtonProperty.frame.origin.y),
(self.ExampleSelectAddButtonProperty.frame.size.width),
(self.ExampleSelectAddButtonProperty.frame.size.height));
[ExamplePopoverController presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
If I place [[self delegate] getUserID] in any function other than viewdidload it works perfectly: returns 100; in viewdidload it returns 0.
What i want to achieve is a delegate to be called automatically when the popover loads. Is viewdidload the best place for it, or is there somewhere else
A couple of people have said this, but haven't been clear enough about the solution.
Instead of doing this:
ExampleController = [[Example alloc] initWithNibName:#"Example" bundle:nil];
ExamplePopoverController = [[UIPopoverController alloc] initWithContentViewController:ExampleController];
[ExampleController setDelegate:self];
Do this:
ExampleController = [[Example alloc] initWithNibName:#"Example" bundle:nil];
[ExampleController setDelegate:self];
ExamplePopoverController = [[UIPopoverController alloc] initWithContentViewController:ExampleController];
What's happening is that -[UIPopoverController initWithContentViewController:] uses its contentViewController's view, causing -[Example viewDidLoad] to be called. Since you haven't yet set the delegate at that point, the delegate is nil, and attempting to call any method on nil returns nil, 0, or 0.0.
Make sure you're actually assigning the delegate property to MainController. For instance, wherever you've initialized Example, you need to set the delegate property:
Example *example = [[Example alloc] init];
example.delegate = self; // Use this if you're initializing Example in MainController
If you're not initializing Example in MainController, instead of "self" use the MainController instance.
In Example.m you need to add line:
[self setDelegate:delegate];
Also, you need to #synthesize delegate;
Hope it help)

Transparent UITableView on top of several UIViewController and OpenGLView (Cocos2D)

Here is my code :
// View Controller with navigation bar
InAppPurchaseViewController *purchaseViewController = [[InAppPurchaseViewController alloc] init];
purchaseViewController.title = #"Magasin";
purchaseViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissViewController:)] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:purchaseViewController] autorelease];
// Add `purchaseViewcontroller` TO container AND container ON openGLView
UIViewController *container = [[UIViewController alloc] init];
[container setView:[[CCDirector sharedDirector] openGLView]];
[container setModalTransitionStyle: UIModalTransitionStyleCoverVertical];
[container presentViewController:navController animated:YES completion:nil];
The UITableView is in purchaseViewController.
I was thinking of using [UIColor clearColor], BUT whatever I use it on I get a BLACK background on my UITableView. The cells get unselectable and unslidable (apart from the elements that are into the cells)
EDIT : The appdelegate
Here is the .h
#class AudioEngine;
#class RootViewController;
#class Score;
#interface AppDelegate : NSObject <UIApplicationDelegate, GameCenterManagerDelegate>
#property int CurrentPackage;
#property int CurrentScore;
#property int CurrentHighScore;
#property BOOL SoundShouldPlay;
#property BOOL PauseScreenUp;
#property(nonatomic, retain) AudioEngine *CustomAudioEngine;
#property(nonatomic, retain) GameCenterManager *CustomGameCenterManager;
#property(nonatomic, retain) UIWindow *window;
#property(nonatomic, readonly) RootViewController *ViewController;
#property(nonatomic, retain) NSString* CurrentLeaderBoard;
#property(nonatomic, retain) NSMutableArray *TenLastScoresArray;
+(AppDelegate *)get;
-(void)connectToGameCenter;
-(void)addScoreToLastScore:(Score*)score;
And the method did finish launching
-(void)applicationDidFinishLaunching:(UIApplication*)application
{
CC_DIRECTOR_INIT();
self.CurrentLeaderBoard = kLeaderboardID;
[[SKPaymentQueue defaultQueue] addTransactionObserver:[InAppPurchaseSingleton sharedHelper]];
[AudioEngine preloadBackgroundMusic];
[AudioEngine playBackgroundMusic:3];
self.SoundShouldPlay = YES;
[SceneManager goSplash];
}
Instead of presenting the view controller on container:
UIViewController *container = [[UIViewController alloc] init];
...
[container presentViewController:navController animated:YES completion:nil];
what should work is presenting it on the root view controller that the cocos2D template created for you. It is normally accessible through the app delegate:
UIViewController *rootViewController = (UIViewController*)[(YOURAPPDELEGATE*)[[UIApplication sharedApplication] delegate] viewController];
[rootViewController presentViewController:navController animated:YES completion:nil];
viewController is an ivar that the cocos2D default template add to the application delegate class. It is normally private, so you will need to define an accessor:
#property (nonatomic, readonly) RootViewController *viewController; //-- .h file
#synthesize viewController; //-- .m file
Hope this helps.
EDIT:
Based on what I have in my app delegate, I think you could try and instantiate the RootViewController like this:
CC_DIRECTOR_INIT
ViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
ViewController.wantsFullScreenLayout = YES;
[ViewController setView:[[CCDirector sharedDirector] openGLView]];
...

iOS Passing (Retain) the value of an NSMutableArray to another NSMutableArray in another view

I'm using .XIB and without ARC. I'm passing the value of the NSMultableArray to another view, if I put [self presentModel...], it works, but if I call the AnotherView with a button the value of the NSMultableArray of the AnotherView is null!
AnotherView.h
#interface AnotherViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *otherAnother;
NSMutableArray *arrayOfTheAnotherView;
}
#property (retain, nonatomic) IBOutlet UITableView *tableView;
#property (retain, nonatomic) NSMutableArray *arrayOfTheAnotherView;
AnotherView.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
otherAnother = [[NSMutableArray alloc]init];
otherAnother = [[NSMutableArray alloc]initWithArray:self.arrayOfTheAnotherView];
//    [otherAnother addObjectsFromArray:arrayOfTheAnotherView];
NSLog(#"%#", self.arrayOfTheAnotherView);
}
The NSLog has written "null"
CurrentView.h
#interface CurrentViewController : UIViewController {
NSMutableArray * arrayCurrentView;
AnotherViewController *superAnotherView;
}
#property (retain, nonatomic) AnotherViewController *superAnotherView;
CurrentView.m
#synthesize superAnotherView;
NSString *x = [[NSString alloc]initWithFormat:#"%#",[label text]];
arrayCurrentView = [[NSMutableArray alloc]init];
[arrayCurrentView retain];
[arrayCurrentView addObject:x];
self.superAnotherView = [[AnotherViewController alloc]initWithNibName:nil bundle:nil];
self.superAnotherView.arrayOfTheAnotherView = [[NSMutableArray alloc]init];
[self.superAnotherView.arrayOfTheAnotherView retain];
[self.superAnotherView.arrayOfTheAnotherView addObjectsFromArray:arrayCurrentView];
I don't know how to retain the value of the NSMultableArray, thanks the help.
It is how I call the AnotherView:
UIButton *buttonAnother = [UIButton buttonWithType:UIButtonTypeCustom]; [buttonAnother setTag:5]; [buttonAnother addTarget:self action:#selector(switchTabBar:) forControlEvents:UIControlEventTouchDown];
[tabBarViewController.view addSubview:buttonAnother];
- (IBAction)switchTabBar:(id)sender { switch ([(UIButton *)sender tag]) { case 5: [self.tabBarController setSelectedIndex:0]; break; }
These should not be necessary:
self.superAnotherView.arrayOfTheAnotherView = [[NSMutableArray alloc]init];
[self.superAnotherView.arrayOfTheAnotherView retain];
Properties are already initialized when the class is initialized.
In fact, your explicit retain lines shouldn't be necessary at all; you're not releasing them as far as I can see and just making the retain count 2 so that it has to be released twice.
I think there's something going on here that the methods and view controllers are not being called in the order you want, probably something to do with the tab bar.
I dicoverd! I'm using the MVC
A array of the APP delegate don't lost its value.
AppDelegate
NSMutableArray *arrayDelegate;
View.m
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate.arrayDelegatePedido addObject:#"title"]; //for example

NSMenuItem with custom view does not update

I have a NSMenuItem with a custom view. However, when I want to update the first row in the menu it add the content as it is in the xib file, not as I set it.
My code:
AppDelegate.m:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
_helper = [[Helper alloc] init];
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setMenu:self.statusMenu];
[statusItem setTitle:#"GravAccount"];
[statusItem setHighlightMode:YES];
_gravatarLoader = [[GravatarLoader alloc] initWithTarget:self andHandle:#selector(setGravatarImage:)];
// Get email address
_email = [_helper getEmailAddress];
if (_email != nil)
{
[_gravatarLoader loadEmail:_email withSize:50.0];
}
}
- (void)setGravatarImage:(NSImage*)image
{
NSLog(#"Image loaded!");
GravatarMenuItem *menuItem = [[GravatarMenuItem alloc] initWithNibName:#"GravatarMenuItem" bundle:nil];
[menuItem.label setTitle:_email];
[menuItem.imageView setImage:image];
NSLog(#"Email: %#", _email);
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:#"" action:nil keyEquivalent:#""];
[item setView:menuItem.view];
[self.statusMenu removeItemAtIndex:0];
[self.statusMenu insertItem:item atIndex:0];
}
GravatarMenuItem.h:
#import <Cocoa/Cocoa.h>
#interface GravatarMenuItem : NSViewController
#property (nonatomic, strong) IBOutlet NSImageView *imageView;
#property (nonatomic, strong) IBOutlet NSTextFieldCell *label;
#end
The outlets are linked in the XIB file on the file's owner.
This is the result:
Log:
2012-03-09 16:57:24.314 appName[51158:403] Image loaded!
2012-03-09 16:57:24.316 appName[51158:403] Email: Paul#******.se
Result:
When you do:
GravatarMenuItem *menuItem = [[GravatarMenuItem alloc] initWithNibName:#"GravatarMenuItem" bundle:nil];
It only creates the view controller. It doesn't actually load the view from the nib. When you access the properties, they are most likely nil as the view has not been loaded (and the connections have not been made).
If you update your code to the following, everything should work fine:
GravatarMenuItem *menuItem = [[GravatarMenuItem alloc] initWithNibName:#"GravatarMenuItem" bundle:nil];
[menuItem view]; // load the view from the nib
[menuItem.label setTitle:_email];
[menuItem.imageView setImage:image];