不使用storyboard创建一个app


我还是直接复制参考链接吧,这样比较省事,我已经跟着做了一遍了,确实可行,但是我改变了backgroundcolor后没有起作用,这个还不知道是怎么回事。


参考链接: 点击打开链接

Here, for example, is what Xcode 6 gives you for a Single View Application:

Xcode 6 Single View Application

You get not only a Main.storyboard file, but also a default LaunchScreen.xib file. Some developers prefer Storyboards, and some do not. It’s relatively easy to modify a project to get back to what a pre-Xcode 6 Empty Application template created; a second modification gets you from there to a “standard” xib-based application, à la Xcode 4. (It should be possible to create custom Xcode 6 templates for both of these types of projects, but that will require some research and experimentation).

The first step is to remove Main.storyboard, LaunchScreen.xib, ViewController.h, and ViewController.m—select them all, right-click, and choose to either remove them from the project, or delete them completely:

Xcode 6 Remove Files

If you run the app at this point, you’ll get an exception: “Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Could not find a storyboard named ‘Main’ in bundle…” This is because the project is trying to use that storyboard file you just removed. This information is in the project’s Info.plist, and the entry is “Main storyboard file base name”:

Xcode 6 Fix PList

To fix this, simply remove the entry (select it and hit delete or hover and click the remove icon). Running the app now will just get you a solid black window, because there’s simply no code present that will create one for you. We’re just one step away from the equivalent of an Xcode 5 Empty Application. Open AppDelegate.m, and edit applicationDidFinishLaunchingWithOptions so that it looks like this:

Running the app at this point will get you a white window, just as it did in Xcode 4 and 5. As well, you’ll see a warning in the output window in Xcode:

Xcode 6 Root View Controller Warning

which also appeared in Xcode 5’s Empty Application, by the way.

At this point, we’re functionally equivalent to an Xcode 5 (or 4) Empty Application. Again, this is the “starting point” used in the Big Nerd Ranch’s iOS programming book. In order to get to the functional equivalent of an Xcode 4 Single View Application, we need to add a View Controller and its corresponding xib file, and hook them up. The first step is to add a subclass of UIViewController and a corresponding xib file. Use ⌘-N or the File->New->File.. menu item to create a new file, and select “Cocoa Touch Class”:

Xcode 6 Add View Controller 1The next dialog asks you to name the class; I cleverly called mine “ViewController”. Make it a subclass of “UIViewController”, and check the “Also create XIB File”:

Xcode 6 Add View Controller 2This will create three files in your project: ViewController.h, ViewController.m, and ViewController.xib:

Xcode 6 Add View Controller 3

Running the app at this point will give you the same behavior as before, as we’ve not yet told your app to actually use the ViewController. To do this, we need to add a property to the AppDelegate, to hold the ViewController; edit AppDelegate.h so it looks like this:

Finally, we need the AppDelegate to create and make use of the ViewController. Add an import directive to AppDelegate.m, so that it knows about the ViewController:

and then edit didFinishLaunchingWithOptions  so it looks like this:

Instead of simply making the window background white, we tell the app to make us a ViewController from the new xib file, and use it as the root view controller (thereby eliminating the warning we saw earlier). At this point, we have what Xcode 4’s Single View Application template created: a minimal, non-storyboard-based app with one xib file.




-------------------------------我是分割线---------------------

这里还有一篇比较好的文章,附带原作者的一些解释。

点击打开链接


Inspired by Julia Evans who I’ve been following awhile and unabashedly posts about things she’s just learning, I’m going to start posting about some stuff I’m just learning! I might not know it all yet and might make mistakes!! This is exciting!!!

Anyway, right now I am trying to build a pretty simple iOS application. The app I’m aiming for is one I could probably build as a web app (in html/css/javascript) pretty fast, but learning Xcode, Objective-C and the entire toolchain seems like a useful thing to learn. I also don’t have a lot of time to really dig in so my time is fractured so starting with a “simple” app seems smart. Anyway, I first tried to use Xcode’s Storyboard and Interface Builder. Storyboard and Interface Builder are both graphical tools built into Xcode (Apple’s IDE for iOS & Mac development) that manage some xml configuration files that under the covers define the views and controls in your screens. I found this kind of infuriating. I have done UI work before so the concepts are not completely strange to me (I’ve done: web programming, client and server, a pebble app, and Windows MFC and GTK apps a long time ago). The problem with Storyboard was that immediately I didn’t understand how what I did in the GUI changed what appeared when I tried running my app. So really I need a map and guides to let me dig in without hiding things that will infuriate me. Maybe later I can use Storyboard.

It turns out, though, that people really want you to use Storyboard and Interface Builder. There don’t seem to be official samples that don’t use them (I picked half a dozen simple looking samples and they all seemed to have .xib or Storyboard files). After much google search term massaging (many of my searches turned up guides that assumed Interface Builder or Storyboard), I turned up a few posts that got me started. The first one got me some basic code so I could get grounded. The second was a great overview “map” of where I was going. The third told me how to remove the generated Storyboard (the new app templates with current Xcode always include a Storyboard).

A little while later, I had “Hello World” without using the GUI tools to construct the layout (and here I am a week or so later posting about it!) Since I had so much trouble finding what I needed, I thought I’d re-write it here. It’s also a form of rubber-duck learning: while I explain this into my editor (to post on the web), I’ll understand it better! On to the steps! First get an empty-ish project setup:

  1. Run Xcode (this is assuming you have it installed which I won’t go into).
  2. Create a new project. Pick Single View Application. Name it whatever.
  3. Delete the Storyboard file. Do this by clicking on it, then pressing the delete key.
  4. In “Supporting Files”, open Info.plist. Highlight the line that mentions the Storyboard.

Now we can go add code! Let’s look at our files. First, because we’re learning, let’s look at where “main” is (what will run first):

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

I already had to go look up some stuff! Even though I know C and C++, I’ve never really looked at Objective-C before. One thing is Objective-C uses “@something” all over for various directives. Objective-C is clearly a stitched together monster of a language but I kind of like that. I assume @autoreleasepool is for some kind of memory management so I’m not worrying about it just yet. But NSStringFromClass([AppDelegate class]) …. huh. The other examples just had text like @"AppDelegate". What’s the difference? The@"AppDelegate" version is just constructing an NSString (versus a bare null-terminated string) with a particular class name. Turns out I want the latter way because it’s interrogating a class to get its name (call class on AppDelegate and construct an NSString from it). This means if I renamed AppDelegate to something else, I’d get a build error rather than a runtime error).

All this is doing is constructing some main framework app (I’d guess with an embedded event loop) that delegates a lot of work to my code. So what does AppDelegate do?

AppDelegate header file:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end

The AppDelegate implementation file:

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end

The examples I was looking at wanted to save off a reference to the window and the main controller, so I modified these two files. AppDelegate header:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *rootController;
@end

The modified AppDelegate implementation:

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.rootController = [[ViewController alloc] init];

    [self.window addSubview:self.rootController.view];
    [self.window setRootViewController:self.rootController];
    
    [self.window makeKeyAndVisible];

    return YES;
}
// left out all the unmodified functions ...
@end

A lot to unpack here! First there’s a window created using the frame (bounding box) generated by asking the UIScreen class for the mainScreen and getting its bounds. TherootController is set with an instance of ViewController which is another class generated by Xcode for the simple project. Then, the window is told what view it has inside it and what its root view controller is. That second line I didn’t have initially and while the code “ran” I got an error and it wasn’t quite working right (the simulator didn’t show the simulated top status bar with time, battery, etc. in it). Clearly this matters somehow, although not critically. I’ll figure it out later! Finally, the ViewController. Its header I didn’t need to modify:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end

The original version of this file didn’t have a lot going on:

#import "ViewController.h"

@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

The default template assumes I’ll be using ‘nibs’ and Storyboard (nibs are files that reflect how you construct the UI in the Interface Builder to define your views). But I want to define the view myself in code. So I needed to modify it to support this:

#import "ViewController.h"

@interface ViewController ()
@end
@implementation ViewController
- (void)loadView {
    CGRect rect = [UIScreen mainScreen].applicationFrame;
    self.view = [[UIView alloc] initWithFrame:rect];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(5,5,200,50)];
    labelView.text = @"Hello world!";
    labelView.textColor = [UIColor blackColor];
    [self.view addSubview:labelView];
}
// leaving out -viewDidLoad and -didReceiveMemoryWarning
@end

This constructs a really basic view that uses the entire space, sets the background color to white, then puts a label showing “Hello world!” on the screen. Looking this over while I type this up, I notice that in two places I’m asking for [UIScreen mainScreen] and doing something with it. Clearly this is probably not quite right and I’ll have to figure that out. But I have a working “hello world!” What did I learn?

  • Lots of basic Objective-C: how strings and properties are created, interfaces, implementation, sending messages (i.e. calling methods), etc.
  • Various UIKit APIs.
  • How to navigate the UIKit docs so I could figure out how to use UILabel and set colors.

And now, hopefully when someone searches the web for “iOS app hello world programmatic view” or something like that they find this useful!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值