我有两个按钮。一个按钮加起来,一个按钮加起来。问题是当我确定一些数字,例如22表示在文本区域时,手机会振动一段时间。这是我的代码:
我想说的是IF标签显示“22”然后振动电话...问题是我如何写这个...我还在学习所以任何有关这方面的帮助将不胜感激!到目前为止,这是我的代码:
#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
答案 0 :(得分:2)
这基本上是Programmatically make the iPhone vibrate
的副本话虽如此,我认为您的代码仍会出错,并且语法似乎已被弃用。
这是一个例子。我没有在测试振动所需的实际iphone上尝试这个,但是如果你将 AudioToolbox框架添加到你的项目中它应该工作,当然你的XIB文件有必要的连接:
<强> 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