关闭标签栏控制器

发布于 2024-10-29 04:36:42 字数 2125 浏览 1 评论 0原文

我对 iPhone 编程还很陌生。我在使用标签栏控制器时遇到了一个无法解决的问题。我有一个带有 2 个选项卡的应用程序,每个选项卡使用单独的类文件和 nib 文件。我使用摇动手势来告诉应用程序执行某些操作,但是当我从选项卡二改回选项卡一时,摇动手机,它会执行第二个视图的操作,而不是当前视图的操作,摇动后当前视图保持不变。

我在这里错过了什么吗?我需要释放一些东西吗?

预先感谢您可以提供的任何帮助。

FirstViewController.m:

    #import "FirstViewController.h"
#import <AudioToolbox/AudioToolbox.h>


@implementation FirstViewController
@synthesize label;

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

    if (fabsf(acceleration.x) > 2.0
        || fabsf(acceleration.y) > 2.0
        || fabsf(acceleration.z) > 2.0) {
            label.text = @"Shake"; 


}


- (void)viewDidLoad {

    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.delegate = self;
    accelerometer.updateInterval = 1.0f/60.0f;
    [super viewDidLoad];

}


- (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 {

}


- (void)dealloc {
    [super dealloc];

}

@end

SecondViewController.m

#import "SecondViewController.h"
#import <AudioToolbox/AudioToolbox.h>

@implementation SecondViewController
@synthesize label2;

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

    if (fabsf(acceleration.x) > 2.0
        || fabsf(acceleration.y) > 2.0
        || fabsf(acceleration.z) > 2.0) {
        label2.text = @"Shake View 2"; 


}


- (void)viewDidLoad {

    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.delegate = self;
    accelerometer.updateInterval = 1.0f/60.0f;
    [super viewDidLoad];

}


- (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 {

}


- (void)dealloc {
    [super dealloc];

}

@end

I'm pretty new to iphone programming. I have come up with a problem when using a tab bar controller that I cannot solve. I have an app with 2 tabs each tab uses separate class files and nib files. I use a shake gesture to tell the app to do something but when I changed from tab two back to tab one shake the phone it does the action of the second view not the current view, the current view remains unchanged after the shake.

Am I missing something here? Do I need to release something?

Thanks in advance for any help you can offer.

FirstViewController.m:

    #import "FirstViewController.h"
#import <AudioToolbox/AudioToolbox.h>


@implementation FirstViewController
@synthesize label;

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

    if (fabsf(acceleration.x) > 2.0
        || fabsf(acceleration.y) > 2.0
        || fabsf(acceleration.z) > 2.0) {
            label.text = @"Shake"; 


}


- (void)viewDidLoad {

    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.delegate = self;
    accelerometer.updateInterval = 1.0f/60.0f;
    [super viewDidLoad];

}


- (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 {

}


- (void)dealloc {
    [super dealloc];

}

@end

SecondViewController.m

#import "SecondViewController.h"
#import <AudioToolbox/AudioToolbox.h>

@implementation SecondViewController
@synthesize label2;

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

    if (fabsf(acceleration.x) > 2.0
        || fabsf(acceleration.y) > 2.0
        || fabsf(acceleration.z) > 2.0) {
        label2.text = @"Shake View 2"; 


}


- (void)viewDidLoad {

    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.delegate = self;
    accelerometer.updateInterval = 1.0f/60.0f;
    [super viewDidLoad];

}


- (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 {

}


- (void)dealloc {
    [super dealloc];

}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜宝宝 2024-11-05 04:36:42

当您在该实现中切换选项卡时,加速度计委托不会更改,因为 viewDidLoad 仅在视图加载到内存中而不是显示时调用。

您可以尝试在 - viewDidAppear:animated 方法中设置它。如果也失败,请查看 UITabBarControllerDelegate 协议和 – tabBarController:didSelectViewController: 方法。首先,您需要创建并分配选项卡栏控制器委托,然后实现该方法。它可能看起来像这样:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
  if([viewController respondsToSelector:@sel(wasSelectedFromTabBar)]){
    [viewController wasSelectedFromTabBar]
  }
}

然后,在视图控制器中,在 wasSelectedFromTabBar 方法中分配加速度计委托。希望它有效!

The accelerometer delegate doesn't change when you switch the tabs in that implementation, because viewDidLoad is only called when the view is loaded into memory and not when displayed.

You could try setting it in the - viewDidAppear:animated method. If that also fails, have a look at the UITabBarControllerDelegate protocol and – tabBarController:didSelectViewController: method. First you need to create and assign the tab bar controller delegate, then implement the method. It could look something like this:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
  if([viewController respondsToSelector:@sel(wasSelectedFromTabBar)]){
    [viewController wasSelectedFromTabBar]
  }
}

Then, in your view controllers, assign the accelerometer delegate in the wasSelectedFromTabBar method. Hope it works!

来世叙缘 2024-11-05 04:36:42

您的视图控制器可能未设置为第一响应者。因此,当您查看选项卡 1 上的视图控制器时,选项卡 2 上的视图控制器正在接收运动事件。请注意,我在这里做了很多假设。

将此代码添加到您的两个视图控制器中,当您切换到每个视图控制器时,它们将接管作为第一响应者。

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}

Your view controllers probably aren't set as the first responder. So when you're looking at the view controller on Tab 1, the view controller on Tab 2 is receiving the motion event. I'm making a lot of assumptions here, mind you.

Add this code to both of your view controllers, and as you switch to each of them they will take over as the first responder.

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文