Activating iPhone alarm and making sound - iphone

First post and first iPhone app in the making here, so please excuse the any n00b mistakes. I'm having trouble activating the alarm. I have a Datepicker and a button as the crux of the code. Here's the .h file
#interface Alarm : UIViewController
#property (retain, nonatomic) NSDate *alarm;
#property (retain, nonatomic) IBOutlet UIButton *sleepButton;
#property (retain, nonatomic) NSTimer *timer;
#property (retain, nonatomic) IBOutlet UIDatePicker *timePicker;
- (IBAction)startAlarm:(id)sender;
- (void)checkAlarm;
#end
And here is the .m file.
- (IBAction)startAlarm:(id)sender
{
self.alarm = [self.timePicker date];
self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:#selector(checkAlarm) userInfo:nil repeats:YES];
{
- (void)checkAlarm
{
// alarm is reached
if ([NSDate date] == self.alarm)
{
// rest of code goes here
}
{
I also have no idea how to make sound and make it repeat itself, so if I can get some guidance on how to do that, that would be great. Thanks!

The problem is here:
if ([NSDate date] == self.alarm)
Dates have millisecond resolution, and your timer only fires once per second - the chances that they align perfectly is rather slim.
Instead you should check:
if ([[NSDate date] compare: self.alarm] == NSOrderedDescending)
to check if the alarm date was passed.

Related

Reactive Cocoa reduce block never gets called

I'm trying to wrap head around Reactive Cocoa, everything works fine except RAC(self.windButton.enabled) sections (at the bottom), reduce blocks never gets called. Would be glad if someone could point me my errors. The code is self-explaining, basically app looks like this:
Here is implementation
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) NSNumber *winding;
#property (strong, nonatomic) NSNumber *minWinding;
#property (strong, nonatomic) NSNumber *maxWinding;
#property (weak, nonatomic) IBOutlet UIButton *windButton;
#property (weak, nonatomic) IBOutlet UIButton *unwindButton;
#property (weak, nonatomic) IBOutlet UILabel *windingLabel;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.winding = #0;
self.minWinding = #0;
self.maxWinding = #15;
RACSignal *windingSignal = RACAble(self.winding);
RACSignal *maxWindingSignal = RACAble(self.maxWinding);
RACSignal *minWindingSignal = RACAble(self.minWinding);
RAC(self.windingLabel.text) = [windingSignal map:^id(NSNumber *winding) {
return winding.stringValue;
}];
[[self.windButton rac_signalForControlEvents:UIControlEventTouchUpInside]
subscribeNext:^(id sender) {
self.winding = #(self.winding.integerValue + 1);
}];
[[self.unwindButton rac_signalForControlEvents:UIControlEventTouchUpInside]
subscribeNext:^(id sender) {
self.winding = #(self.winding.integerValue - 1);
}];
RAC(self.windButton.enabled) = [RACSignal
combineLatest:#[windingSignal, maxWindingSignal]
reduce:^(NSNumber *winding, NSNumber *maxWinding) {
return #(winding.intValue < maxWinding.intValue);
}];
RAC(self.unwindButton.enabled) = [RACSignal
combineLatest:#[windingSignal, minWindingSignal]
reduce:^(NSNumber *winding, NSNumber *minWinding) {
return #(winding.intValue > minWinding.intValue);
}];
}
My problem is that buttons never get disabled.
I had to use RACAbleWithStart to pre-fill signal with initial values since combineLatest method waits until every provided signal has some value.
RACSignal *windingSignal = RACAbleWithStart(self.winding);
RACSignal *maxWindingSignal = RACAbleWithStart(self.maxWinding);
RACSignal *minWindingSignal = RACAbleWithStart(self.minWinding);
The RAC() macro takes two arguments at a minimum.
check out this question

Error SIGBART in XCode iOS

I have a program with different components, but when I compile, I get signal SIGBART every time. The main purpose of my code is to change a boolean when the user moves, set text to a variable when a button is tapped, and set a string to do/don't with the press of different buttons. My code is below:
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize mapView;
#synthesize dodont;
#synthesize reminders;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.showsUserLocation = YES;
on = #"don't";
dodont.text = on;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:
(MKUserLocation *)userLocation
{
move = TRUE;
}
- (IBAction)enable:(id)sender {
on = #"do";
}
- (IBAction)disable:(id)sender {
on = #"don't";
}
- (IBAction)save:(id)sender {
content = reminders.text;
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
NSString *on;
BOOL move= FALSE;
NSString *content;
#interface ViewController : UIViewController {
MKMapView *mapview;
}
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
- (IBAction)enable:(id)sender;
- (IBAction)disable:(id)sender;
#property (weak, nonatomic) IBOutlet UILabel *dodont;
- (IBAction)save:(id)sender;
#property (strong, nonatomic) IBOutlet UITextView *reminders;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (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, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the 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. Save data if appropriate. See also applicationDidEnterBackground:.
}
#end
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
Don't you want to put this stuff:
NSString *on;
BOOL move= FALSE;
NSString *content;
between #interface and #end in your .h file?
And don't forget for: #property (nonatomic, strong) for each property (BOOL value have no strong parameter).
Your interface should look like this:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController {
MKMapView *mapview;
}
#property (strong, nonatomic) NSString *on;
#property (nonatomic) BOOL move;
#property (strong, nonatomic) NSString *content;
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property (strong, nonatomic) IBOutlet UITextView *reminders;
#property (weak, nonatomic) IBOutlet UILabel *dodont;
- (IBAction)enable:(id)sender;
- (IBAction)disable:(id)sender;
- (IBAction)save:(id)sender;
#end
In iOS/Objective-C, variables should be declared like below given way :
#interface Classname:SuperClass
{
#Declare variables here
}
Set property for variables here
declare methods here
#end
And you are declaring the variables above the class declarations.
Also you haven't initialized the window for the application in applicationdidFinishLaunching method. Try this and setting the window rootviewcontroller with the view you want to load.

XIB doesn't load... just shows a black screen

I'm creating a universal application in iOS 4.2 and above.
I have a nib called DataInputViewController_iPhone.xib. On the first view controller which comes up I have a button which should launch the class DataInputViewController. It does it, but it doesn't show the contents of the XIB file. It just shows a black screen.
Any ideas why this happens?
I have this code which calls the new view controller:
-(IBAction)clickedButton:(id)sender {
if((UIButton *)sender == (UIButton *)startButton){
NSString *nibName;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
nibName = #"DataInputViewController_iPhone";
} else {
nibName = #"DataInputViewController_iPad";
}
DataInputViewController *nextVC = [[DataInputViewController alloc] initWithNibName:nibName bundle:nil];
[self.navigationController pushViewController:nextVC animated:YES];
}
else if((UIButton *)sender == (UIButton *)otherButton){
NSLog(#"clicked the other button");
}
}
NSLogging in this method and in the init method of the new view controller tells me that the nibNameOrNil variable is pulled through correctly.
This is what the (standard) init method looks like in DataInputViewController.m:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
Any ideas why this is happening? Or how to fix?
Thanks :)
You don't have to manually set up the nib name. Just name them DataInputViewController~iphone.xib and DataInputViewController~ipad.xib, then call [[DataInputViewController alloc] init].
See iOS Supports Device-Specific Resources for more information.
I had the same problem. Just clean your project, close XCode, reboot your Mac and open project again.
And don't forget delete app on simulator or device previously.
The only way I have found to fix this is to delete the nibs and class and then recreate them. Not particularly elegant, but it works ...
I had this same issue where I needed to load either iPad or iPhone .xib files for a utility view controller. It is a form so it had a lot of UILabel and UITextField elements that were linked as IBOutlets. I would construct the entire layout, link all of the elements, and it worked fine until when I built and ran it. I even took the advice posted on this thread and re-created both .xib's and the class files (a great deal of linking IBOutlets) with no success.
The problem was this. The root view cannot be #synthesized in the implementation of the class. As soon as I synthesized the base view, everything went black. By simply removing the "#synthesize view;" line, everything worked like a charm. When I presented my view controller everything was there again.
Here is the header file for the class with all of the IBOutlets:
#import <UIKit/UIKit.h>
#interface SignUpFormViewController : UIViewController <UITextFieldDelegate, NSXMLParserDelegate>
#property (strong, nonatomic) IBOutlet UIView *rootView;
#property (weak, nonatomic) IBOutlet UILabel *signUpFormLabel;
#property (weak, nonatomic) IBOutlet UIButton *infoButton;
#property (weak, nonatomic) IBOutlet UILabel *enterEmailLabel;
#property (weak, nonatomic) IBOutlet UITextField *enterEmailTextField;
#property (weak, nonatomic) IBOutlet UILabel *enterPasswordLabel;
#property (weak, nonatomic) IBOutlet UITextField *enterPasswordTextField;
#property (weak, nonatomic) IBOutlet UILabel *reEnterPasswordLabel;
#property (weak, nonatomic) IBOutlet UITextField *reEnterPasswordTextField;
#property (weak, nonatomic) IBOutlet UILabel *enterFirstNameLabel;
#property (weak, nonatomic) IBOutlet UITextField *enterFirstNameTextField;
#property (weak, nonatomic) IBOutlet UILabel *enterLastNameLabel;
#property (weak, nonatomic) IBOutlet UITextField *enterLastNameTextField;
#property (weak, nonatomic) IBOutlet UILabel *enterPhoneNumberLabel;
#property (weak, nonatomic) IBOutlet UITextField *enterPhoneNumberTextField;
#property (weak, nonatomic) IBOutlet UILabel *enterStreetAddressLabel;
#property (weak, nonatomic) IBOutlet UITextField *enterStreetAddressTextField;
#property (weak, nonatomic) IBOutlet UILabel *enterStreetAddress2Label;
#property (weak, nonatomic) IBOutlet UITextField *enterStreetAddress2TextField;
#property (weak, nonatomic) IBOutlet UILabel *enterCityLabel;
#property (weak, nonatomic) IBOutlet UITextField *enterCityTextField;
#property (weak, nonatomic) IBOutlet UILabel *enterStateLabel;
#property (weak, nonatomic) IBOutlet UITextField *enterStateTextField;
#property (weak, nonatomic) IBOutlet UILabel *enterZipLabel;
#property (weak, nonatomic) IBOutlet UITextField *enterZipTextField;
#property (weak, nonatomic) IBOutlet UIButton *backButton;
#property (weak, nonatomic) IBOutlet UIButton *signUpNowButton;
- (IBAction)infoButtonPressed:(id)sender;
- (IBAction)backButtonPressed:(id)sender;
- (IBAction)signUpNowButtonPressed:(id)sender;
#end
Here is the implementation file with the root view being synthesized:
#import "SignUpFormViewController.h"
#implementation SignUpFormViewController
#synthesize rootView,signUpFormLabel,infoButton,enterEmailLabel,enterEmailTextField,enterPasswordLabel,enterPasswordTextField,reEnterPasswordLabel,reEnterPasswordTextField,enterFirstNameLabel,enterFirstNameTextField,enterLastNameLabel,enterLastNameTextField,enterPhoneNumberLabel,enterPhoneNumberTextField,enterStreetAddressLabel,enterStreetAddressTextField,enterStreetAddress2Label,enterStreetAddress2TextField,enterCityLabel,enterCityTextField,enterStateLabel,enterStateTextField,enterZipLabel,enterZipTextField,backButton,signUpNowButton,btnDone,btnPrev,btnNext,inputAccView,activeTextField,currentString;
Simply remove the "rootView" from the list of properties being synthesized and this should fix the problem. Much easier than deleting the .xib files for BOTH iPad and iPhone layouts and re-creating them.
FYI I am using xCode 4.6.3 with target SDK = 6.1
I hope this helps anybody else that experiences this time-consuming and very frustrating issue.
This may help you: delete your app on the simulator or device and run again. I find if I twiddle with nibs too much, especially around names or localization, it appears that everything works (I get no errors), but Xcode is actually ignoring the changes.
So perform a Clean (Product->Clean), delete from simulator or device, run again.
You have to make sure to implement the loadView method in your DataInputViewController.m and inside the method add the line [super loadView], like this:
- (void)loadView {
[super loadView];
}
I have found that not dragging an outlet from the File Owner to your custom sub-classed UITableViewCell.h or .m file can cause the blackness as well. Make sure that you drag an outlet to one of the two files associated with your sub-class of UITableViewCell.
In my case,
I was receive xib file from co-worker.
well loaded xib but did not shown.
spent all my one a day because Hidden checkbox was checked
check Hidden checkbox in xib.
Simple Fix!!
Navigating to version editor and back to standard editor fixed the problem for me

CGRectIntersectsRect problems, overlapping frames but not detecting it

I am trying to create an app for iPad and it's been fine so far, and now i am trying to make an object jump, so i've made it that it jumps up into the air and then falls down again, and thats working fine. but my problem comes when i want it to stop at the ground again, i thought CGRectIntersectsRect would work but it just doesn't work, i mean the object is clearly overlapping the ground image, but it's not stopping.
Code:
-(void)gameStatePlayNormal{
if(cubeState == CubeIsTouchingGround){
cubeVelocity.y = 0;
}
gravity = CGPointMake(0, kGravity);
cube.center = CGPointMake(cube.center.x, cube.center.y + cubeVelocity.y);
cubeVelocity.y += gravity.y;
if(touchState == Touch){
if(cubeState != CubeIsJumping){
cubeState = CubeIsJumping;
cubeVelocity.y = cubeVelocity.y -10;
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(cubejump) userInfo:nil repeats:NO];
}
}
}
-(void)cubejump{
if(cubeState == CubeIsJumping){
if(CGRectIntersectsRect(cube.frame, ground.frame)){
NSLog(#"Cube is touching the ground.");
cubeState = CubeIsTouchingGround;
cubeVelocity.y = 0;
}
}
}
and the .h file:
UIImageView *cube;
UIImageView *spike;
UIImageView *box;
UIImageView *ground;
}
-(void)gameStatePlayNormal;
#property (nonatomic, retain) IBOutlet UIImageView *cube;
#property (nonatomic, retain) IBOutlet UIImageView *spike;
#property (nonatomic, retain) IBOutlet UIImageView *box;
#property (nonatomic, retain) IBOutlet UIImageView *ground;
and of course everything is linked up in the .xib file, i can manipulate both the ground image and the cube image in other ways so those are linked to the variable in the xib file.
please help, i tried using the self.view.bounds.size.width as well but it didn't work for the lower part of the screen. please help :)

Possibilities to use NSTimer global

I am implementing an iPhone application. In that I need to use a timer to call a method to get the values frequently. And Also I need to stop the Timer in another method. So I need to use it as global. But I don't an idea to create a global Timer can you guys please help on this.
Thanks in Advance,
Sekhar Bethalam.
Just keep a reference to the timer.
In .h file:
NSTimer *timer;
..
#property (nonatomic, retain) NSTimer *timer;
In .m file:
#synthesize timer;
..
// Initiation:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFireMethod:) userInfo:nil repeats:YES];
..
// Termination:
[self.timer invalidate];
In the interface, declare a property:
#interface myViewController : UIViewController
{
NSTimer *timer;
}
#property (nonatomic, retain) NSTimer *timer;
#end
and then synthesise it in the .m file:
#implementation myViewController
#synthesize timer;
and then whenever you need to use it, just use timer.