iOS:如何将涉及nib的代码转换为故事板代码?

时间:2017-08-14 16:02:34

标签: ios objective-c cocoa-touch storyboard

最近我开始学习iOS编程,我在Objective-C中找到了一些关于iOS编程的书籍。这本书(学习iOS编程,2013)有一个部分,演示了生成表视图应用程序的方法,但我在书中的一些代码中陷入困境。

例如,下面使用的书在AppDelegate.h的didFinishLaunchingWithOptions中编码,以实现所需的表。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

City *london=[[City alloc] init];
london.cityName=@"London";
london.cityDescription=@"The capital of the United Kingdom and England.";
london.cityPicture=[UIImage imageNamed:@"London.jpg"];

City *sanFrancisco=[[City alloc] init];
sanFrancisco.cityName=@"San Francisco";
sanFrancisco.cityDescription=@"The heart of the San Francisco Bay Area.";
sanFrancisco.cityPicture=[UIImage imageNamed:@"SanFrancisco.jpg"];


City *sydney=[[City alloc] init];
sydney.cityName=@"Sydney";
sydney.cityDescription=@"The largest city in Australia.";
sydney.cityPicture=[UIImage imageNamed:@"Sydney.jpg"];

City *madrid=[[City alloc] init];
madrid.cityName=@"Madrid";
madrid.cityDescription=@"The capital and largest city of Spain.";
madrid.cityPicture=[UIImage imageNamed:@"Madrid.jpg"];

self.cities=[[NSMutableArray alloc] initWithObjects:london,sanFrancisco,sydney,madrid, nil];

self.viewController=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController=self.viewController;
[self.window makeKeyAndVisible];

/*UIStoryboard *test=[UIStoryboard storyboardWithName:@"Main"        bundle:nil];
self.viewController=(UIViewController *) [test instantiateViewControllerWithIdentifier:@"ViewController"];
self.window.rootViewController=self.viewController;
[self.window makeKeyAndVisible];*/
return YES;

}

AppDelegate.h的代码是这样的:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;
@property (strong, nonatomic) NSMutableArray *cities;


@end

所以我第一次按照书中的插图,但应用程序崩溃并告诉我“无法在捆绑中加载NIB”。然后我在这里检查一些类似的问题,并知道NIB布局设计被故事板取代(我写了一些代码来尝试帮助解决问题,但它没有用,请查看上面的评论代码)。如何将上述代码转换为与storyboard兼容的代码,尤其是“initwithNibName”,以便产生相同的结果?因为我只有用于生成布局的故事板文件(我的Xcode是8.3.3,我找不到任何方法来实现nib文件)。感谢。

0 个答案:

没有答案