Xcode Changes Unmodified Storyboard and Xib Files

Xcode changes unmodified storyboard and XIB files

This is not a bug, this is a consequence of how Xcode processes storyboard files.
I am writing a diff and merge program for storyboard files (GitHub link) and I have spent hours analyzing the storyboard files logic and how Xcode processes it. This is what I discovered:

  • Why do weird changes occur in storyboard files?
    Xcode uses the NSXML API to parse storyboard files into some NSSet-based logical tree structure. When Xcode needs to write changes it creates an NSXMLDocument based on the logical tree structure, clears the storyboard file and calls XMLDataWithOptions: to fill the file again. Because sets do not preserve the order of their elements, even the slightest modification could change the whole storyboard XML file.

  • Why does the class tag disappear or reappear randomly?
    The <class> section is nothing more than an internal Xcode cache. Xcode use it to cache information about classes. The cache changes often. Elements are added when class .h/.m files are opened and removed when Xcode suspects they are outdated (at least older Xcodes behave like this). When you save the storyboard, the current version of the cache is dumped, which is why the <class> section often changes or even disappears.

I have not reverse-engineered Xcode; I made these observations by experimenting with Xcode and storyboard files. Nevertheless, I am almost 100% sure it works this way.

Conclusions:

  • Cache section is unimportant; you can safely ignore any change in it.
  • Contrary to what you can find on all forums, merging storyboards files is not a complicated task. For example, let’s assume you changed MyController1 view controller in a storyboard document. Open the storyboard file, and find something like this
    <viewController id=”ory-XY-OBM” sceneMemberID=”MyController1”>.
    You can safely commit only changes in this section and ignore everything else. If you changed segues or constraints, also commit anything that has “ory-XY-OBM” inside. Simple!

How to avoid Xcode gratuitous edits to storyboard files?

Apple says it's a known issue:

This is a follow up to Bug ID# 9847336. After further investigation it has been determined that this is a known issue, which is currently being investigated by engineering. This issue has been filed in our bug database under the original Bug ID# 9056156. The original bug number being used to track this duplicate issue can be found in the State column, in this format: Duplicate/OrigBug#.

How can i use xib files instead of storyboard in Xcode 5?

add new file in that select the cococatouch and select the Objective -C class and then go to the next click.

you show the below screen

at the bottom must select the with xib for user Interface.and next

AppDelegate.h:

@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
ViewController *viewObj;
UINavigationController *navObj;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewObj;
@property (strong, nonatomic) UINavigationController *navObj;

AppDelegate.m:

@synthesize viewObj,window,navObj;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
viewObj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
navObj = [[UINavigationController alloc] initWithRootViewController:viewObj];

window.rootViewController=navObj;
[window makeKeyAndVisible];

return YES;
}

Sample Image

App Shows Old XIB Instead of Storyboard

In the ViewController that I was segueing to, I called a custom function which returned a reference to a newly created object of the ViewController class itself:

-(id) initWithTitle:(NSString *) title
{
self = [super init];
if (self)
{
self.title = title;

// Load the dictionary
self.dictionary = [NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"NewDictionary"
ofType:@"plist"]];
}

return self;
}

It was left there from the old code, and I completely overlooked it. It was no longer needed now that I was using Storyboard. So instead of seeing the ViewController that I was segueing into, it was showing the old XIB file. Deleting the XIB file from the project showed an empty, black view, and removing the code [super init] showed the correct Storyboard ViewController.

Although I got it working, I'm still not certain as to why it was showing the old XIB file, even though I removed references to it.

Removing Storyboard to use XIB files

I've had to do this a couple of times. You'll want to make sure you add your AppDelegate to your MainWindow.xib file you are using to replace the storyboard. Make sure you get all of your delegates and windows connected properly so you have an appropriate outlet on startup.

MainWindow.xib

  • AppDelegate added (connected to Window and Delegate)
  • Window added (and connected to the Window in your AppDel)
  • File Owner (connected to delegate)

Info.plist

  • Main nib file base name (set to your MainWindow.xib)

I basically took an older project and made sure things were connected properly and my MainWindow looked correct. If you haven't added a UIWindow to your AppDelegate, you'll need to do that too.

Sample Image

If you have the info on the crash, we can likely help you further.



Related Topics



Leave a reply



Submit