I am creating an alarm clock for myself and need to tell the app that the alarm is active and so I figure I will just put in a UISwitch. Also, I figure that I can just have the switch change the value of my BOOL so that no matter what view I am in I can still check the state of the switch.
I have an IBOutlet UISwitch in the .h file that I have connected to the UISwitch in Interface Builder as well as connecting the IBAction to the switch....
When I check the console all I see when I switch the switch is this:
I am creating an alarm clock for myself and need to tell the app that the alarm is active and so I figure I will just put in a UISwitch. Also, I figure that I can just have the switch change the value of my BOOL so that no matter what view I am in I can still check the state of the switch.
I have an IBOutlet UISwitch in the .h file that I have connected to the UISwitch in Interface Builder as well as connecting the IBAction to the switch....
When I check the console all I see when I switch the switch is this:
What am I doing wrong? Why isn't the value of the BOOL changing?
I have been researching for days now and can't seem to find anything helpful.
Any help GREATLY appreciated. Please & Thank You
You show the property declaration for normalAlarmPower, but how do you create the getter and setter? Do you use @synthesize, or create custom getter and setter methods? Show us that.
Also, in your normalAlarmState method, print the value of your appDelegate variable. If that's nil, you'll always get zero for any property.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
You show the property declaration for normalAlarmPower, but how do you create the getter and setter? Do you use @synthesize, or create custom getter and setter methods? Show us that.
Also, in your normalAlarmState method, print the value of your appDelegate variable. If that's nil, you'll always get zero for any property.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Where you need to replace AppDelegateClass with the class name of your custom app delegate.
That's all there is to it.
You could also create an instance of your view controller in your mainWindow.xib, and connect your view controller's appDelegate property to the application delegate in interface builder. That's what I usually do.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Where you need to replace AppDelegateClass with the class name of your custom app delegate.
That's all there is to it.
You could also create an instance of your view controller in your mainWindow.xib, and connect your view controller's appDelegate property to the application delegate in interface builder. That's what I usually do.
When I connect the application delegate to appDelegate in the in the MainWindow.xib.....nothing happens.
I did that and then run the app and get the same response: "(null)"
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Ive tried what you said and then gone back and tried putting "appDelegate = (AlarmClockAppDelegate)[UIApplication sharedApplication].delegate;" in the AlarmNormalViewController.m file and it was able to find the appDelegate declaration but it still gave the non-scalar type error.
Here is the code I have have a look cuz I'm kinda lost.... :
AlarmNormalViewController.h
#import "AlarmClockAppDelegate.h"
#import "MainViewController.h"
@implementation AlarmClockAppDelegate
@dynamic normalAlarmPower;
@synthesize window;
@synthesize mainViewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the main view controller's view to the window and display.
[window addSubview:mainViewController.view];
[window makeKeyAndVisible];
return YES;
}
-(void)viewdidload {
appDelegate = (AlarmClockAppDelegate)[UIApplication sharedApplication].delegate;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)dealloc {
[mainViewController release];
[window release];
[super dealloc];
}
@end
Ive tried what you said and then gone back and tried putting "appDelegate = (AlarmClockAppDelegate)[UIApplication sharedApplication].delegate;" in the AlarmNormalViewController.m file and it was able to find the appDelegate declaration but it still gave the non-scalar type error.
Here is the code I have have a look cuz I'm kinda lost.... :
--snip---
Oh lordy. You are totally lost, aren't you?
There are several things wrong that I can see.
The system doesn't expect a viewDidLoad method in your app delegate unless it's also a view controller, which yours is not.
Thus, the viewDidLoad you put in your app delegate will never be called.
You need to have your AlarmNormalViewController class be able to get to the app delegate. Setting an "appDelegate" variable in your app delegate doesn't do you any good, because any object can always refer to itself as "self"
You want the following line in your AlarmNormalViewController object's viewDidLoad method:
You were missing an asterisk in the type cast. (I marked the change in bold)
You need to stop flailing around, slapping code here and there without any real idea of what you're doing and do some studying. I'd suggest buying a book that teaches you beginning iPhone development and going through every exercise in the first few chapters until you understand the basics. Pay particular attention to memory management. If you don't understand object owernship, when to retain, when to release, and how to use properties, your code is going to crash in weird places and leak memory like a sieve.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
The system doesn't expect a viewDidLoad method in your app delegate unless it's also a view controller, which yours is not.
Thus, the viewDidLoad you put in your app delegate will never be called.
You need to have your AlarmNormalViewController class be able to get to the app delegate. Setting an "appDelegate" variable in your app delegate doesn't do you any good, because any object can always refer to itself as "self"
You want the following line in your AlarmNormalViewController object's viewDidLoad method:
You were missing an asterisk in the type cast. (I marked the change in bold)
You need to stop flailing around, slapping code here and there without any real idea of what you're doing and do some studying. I'd suggest buying a book that teaches you beginning iPhone development and going through every exercise in the first few chapters until you understand the basics. Pay particular attention to memory management. If you don't understand object owernship, when to retain, when to release, and how to use properties, your code is going to crash in weird places and leak memory like a sieve.
oh ok however after I add that now my app crashes when I flip the switch. There is apparently more that I'm missing....is there any particular book your suggest that will help me out...
oh and here is the the console readout when it crashes: