#import "OtherController.h"
#import "ViewController.h"
ViewController *viewController;

@interface OtherController ()

@end

@implementation OtherController

- (void)viewDidLoad
{
    [super viewDidLoad];
    viewController = [[ViewController alloc] initWithNibName:@"viewController" bundle:nil];
}

- (IBAction)levelNormal{
    viewController.level = 1;
    [self.navigationController pushViewController:viewController animated:YES];

}

我尝试了各种关于stackoverflow的技巧,尽管它们似乎都无法正常工作。我试图在单击按钮时打开一个视图,然后在打开视图时,根据按下哪个按钮,视图应具有不同的“级别”编号。就我而言,我没有NavigationController。我已经阅读了有关将代码粘贴到AppDelegate类中的不同文章,尽管这对我也不起作用。当我调试按钮的按下操作时,我看到该值已更改,但是在尝试更改视图时没有其他反应。

这是ViewController头文件:
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *resumeButton;
@property (weak, nonatomic) IBOutlet UIImageView *opacityScreen;
@property (weak, nonatomic) IBOutlet UILabel *scoreField;
@property (weak, nonatomic) IBOutlet UIButton *pauseButton;
@property (weak, nonatomic) IBOutlet UIButton *mainMenuButton;
@property (weak, nonatomic) NSTimer * nst;
@property(nonatomic) int level;
@end

最佳答案

在AppDelegate的didFinishLaunchingWithOptions中创建UINavigationController对象,然后将UIViewcontroller对象分配为rootViewController,如下所示,并将navObj传递到窗口根视图。

viewController = [[ViewController_iPhone alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];UINavigationController *navObj = [[UINavigationController alloc] initWithRootViewController:viewController];[self.window setRootViewController:navObj];

10-04 10:18