gpt4 book ai didi

iphone - 切换 xib View 而不携带上一屏幕的命令

转载 作者:行者123 更新时间:2023-11-29 04:43:12 25 4
gpt4 key购买 nike

自从我开始使用 xCode 以来,我就一直在使用 Storyboard。今天我一直在尝试使用 xib 文件的旧方法。

我的第一个 View 有一个加速计代码,当我在第二个 View 上时该代码仍然处于事件状态。

有没有办法阻止第二个 View Controller 使用第一个 View 中的代码?

我正在将第二个 View 头文件导入到第一个 View 实现文件中,这是正确的吗?如果我删除该导入,则会出现错误。

//
// ViewController.m



#import "ViewController.h"
#import "ViewController2.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad
{

[super viewDidLoad];

}

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

-(void)viewWillAppear:(BOOL)animated{
[self startAccel];
//[self view];

}

-(void)viewWillDisappear:(BOOL)animated{
[self stopAccel];
//[self view];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));

}



-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{

double const kThreshold = 1.7;
// double const kThreshold = 2.0;
if ( fabsf(acceleration.x) > kThreshold
|| fabsf(acceleration.y) > kThreshold
|| fabsf(acceleration.z) > kThreshold){


int randomNumber = arc4random() % 3 + 1;

NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"Sound%02d", randomNumber] ofType:@"wav"]];

AVAudioPlayer * soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];

[soundPlayer prepareToPlay];
[soundPlayer play];

}
}


-(void)startAccel{
UIAccelerometer * accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = .25;
}

-(void)stopAccel{
UIAccelerometer * accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = nil;
}




-(IBAction)View2:(id)sender;{
ViewController2 *V2 = [[ViewController2 alloc]
initWithNibName:@"ViewController2"
bundle:nil];

[self.view addSubview:V2.view];
}


@end

最佳答案

第二个 Controller 并不真正使用第一个 Controller 的代码,但是,如果您在显示第一个 View 时调用了 startAccelerometerUpdates,则您可能需要在即将呈现第一个 View 时停止它们。第二个(如果不需要的话)。

关于iphone - 切换 xib View 而不携带上一屏幕的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10050738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com