简体   繁体   English

在Xcode上创建一个项目后,我删除了默认提供的viewController并创建了一个新的。 但是项目没有运行

[英]After creating a project on Xcode, i deleted the viewController that comes by default and created a new one. But the project does not run

I created a project in Xcode and deleted the viewController file that comes by default in it. 我在Xcode中创建了一个项目,并删除了其中默认包含的viewController文件。 I added a new file(objective-c class) and added some controls in its viewDidLoad and run the project but it does not seem to work,the view it shows is blank. 我添加了一个新文件(objective-c类),并在其viewDidLoad中添加了一些控件并运行该项目,但它似乎不起作用,显示的视图为空白。 Is there any setting to be made? 有什么需要设置的吗?

When you say deleted the viewController file that comes by default in it. 当您说deleted the viewController file that comes by default in it. , that means that you have chosen application type other than Empty Application . ,表示您选择的应用程序类型不是Empty Application That means most probably you might have chosen a Single view Application , as the defaultViewcontroller file will be provided with SingleView application( means with Storyboards by defaults on Xcode 5+). 这意味着您很可能已经选择了“ Single view Application ,因为SingleView应用程序将提供defaultViewcontroller文件(这意味着Xcode 5+上默认情况下带有Storyboard)。

So even if you have deleted the application, you can just 因此,即使您删除了该应用程序,也可以

  • add a NewViewController (subclassed from UIViewController ), 添加一个NewViewController(从UIViewController继承),
  • name it ( myViewController ) 命名为( myViewController
  • save it 保存
  • Import it in appDelegate File. 将其导入到appDelegate文件中。 ( #import "myViewController.h" ) #import "myViewController.h"
  • Create an instance of it.( myViewController *vc = [myViewController alloc]init] ) 创建它的一个实例。( myViewController *vc = [myViewController alloc]init]
  • Push it on navigationController 将其推入navigationController

If you want to add buttons or anything else, 如果您想添加按钮或其他内容,

  • just create objects in myViewController 只需在myViewController创建对象
  • & instantiate them 并实例化它们
  • add them to SubViews. 将它们添加到SubViews。


    If you are using storyBoards, then 如果您使用的是StoryBoards,则

    • Add a new ViewController file & name it, something like myViewController 添加一个新的ViewController文件并命名,类似于myViewController
    • Add a new ViewControlelr object in the mainStoryboard file by drag & drop. 通过拖放将新的ViewControlelr对象添加到mainStoryboard文件中。
    • Select its inheriting class in the inspector on RHS, as myViewController 在RHS的检查器中选择其继承类,作为myViewController
    • No need to modify anything in appDelegate file. 无需修改appDelegate文件中的任何内容。
    • Make that controller in storyBoard as entryPoint. 在StoryBoard中将该控制器设置为entryPoint。 A default arrow will be preceding your controller. 默认箭头将在您的控制器之前。

hope that gives you the overview. 希望能给您概述。

You have to set mainviewcontroller from didFinishLaunchingWithOptions method of appdelegate.m file. 您必须从appdelegate.m文件的didFinishLaunchingWithOptions方法设置mainviewcontroller。

as below 如下

#import "Yourviewcontroller.h"

and in didFinishLaunchingWithOptions method:- didFinishLaunchingWithOptions方法中:

    self.viewController = [[YourViewControoler alloc] initWithNibName:@"Your xib file" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = self.navigationController;

I hope this will help you. 我希望这能帮到您。

Yes. 是。 You have modify the AppDelegate file. 您已修改AppDelegate文件。

Suppose you add the new controller that name is DashboardViewController. 假设您添加了名为DashboardViewController的新控制器。

DashboardViewController.h DashboardViewController.h

#import <UIKit/UIKit.h>

@class DashboardViewController;

@property (nonatomic,strong) DashboardViewController *viewController;


***DashboardViewController.m***



@synthesize viewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
         self.viewController = [[DashboardViewController alloc] initWithNibName:@"DashboardViewController" bundle:nil];
   self.window.rootViewController = self.viewController;
   self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM