-3

Possible Duplicate:
Making the iPhone vibrate

I have two buttons. One button adds up, one button adds down. The question is when I certain number such as lets say 22 is in the text area, the phone vibrates for a certain mount of time. Here is my code for it:

What I am trying to say is IF Label Displays "22" THEN VIBRATE PHONE... The question is how do i go about writing this.. I'm still learning so any help regarding this would be much appreciated! Here is my code so far:

#import "StartCountViewController.h"
#import "AudioToolbox/AudioServices.h"

@implementation StartCountViewController


int Count=0;

-(void)awakeFromNib {

    startCount.text = @"0";

}


- (IBAction)addNumber {

    if(Count >= 999) return;

    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", Count++];
    startCount.text = numValue;
    [numValue release];

}

- (IBAction)vibrate {


}
- (IBAction)subtractNumber {

    if(Count <= -35) return;

    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", Count--];
    startCount.text = numValue;
    [numValue release]; 
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
Community
  • 1
  • 1
Chase Chiangi
  • 61
  • 1
  • 7

1 Answers1

2

This is basically a duplicate of Programmatically make the iPhone vibrate

Having said that, I think your code is still going to have errors, and the syntax seems deprecated.

Here's an example. I didn't try this on an actual iphone which would be required to test the vibration, but it should work provided you add the AudioToolbox framework to your project, and of course your XIB file has the necessary connections:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *numberLabel;
- (IBAction)addNumber:(id)sender;
- (IBAction)subtractNumber:(id)sender;
@end

ViewController.m

#import "ViewController.h"
#import "AudioToolbox/AudioServices.h"

@interface ViewController ()
{
  int count;
}
@end

@implementation ViewController
@synthesize numberLabel;

- (void)viewDidLoad
{
  count = 0;
  [self updateCount];
  [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
  [self setNumberLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc 
{
  [numberLabel release];
  [super dealloc];
}

- (IBAction)addNumber:(id)sender 
{
  if(count >= 999) {
    return [self vibrate];
  }; // ignore numbers larger than 999
  count++;
  [self updateCount];
}

- (IBAction)subtractNumber:(id)sender 
{
  if(count <= -35) {
    return [self vibrate];
  }; // ignore numbers less than -35
  count--;
  [self updateCount];
}

-(void)updateCount 
{
  NSString *countStr = [[NSString alloc] initWithFormat:@"%d",count];
  [self.numberLabel setText:countStr];
  [countStr release];
}

-(void)vibrate 
{
  NSLog(@"I'm vibrating");
  AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
@end
Community
  • 1
  • 1
FilmJ
  • 2,011
  • 3
  • 19
  • 27