MacOS-MacAPP uses Main.storyboard to start the view program stepping on the pit

Since the main focus is on iOS development of mobile APPs, there are fewer APP developments on the Mac side. I also thought that it would be similar to iOS.

valiant, high-spirited

I didn't expect it, I really didn't expect it, it's only 3 seconds handsome! ! ! Awkward! ! !

It hangs at the startup entrance of the program view, stepped on some pitfalls, and I will not introduce the steps of creating a macOS project. You can refer to the blog post

I used the macOS project created by OC, and checked the storyboard. After creating the project, the interface is as follows:

What you see in the visual editor. There are three main areas, each of which also has a corresponding textual representation in the Document Outline:

  • Application Scene : menu bar and items.
  • Window Controller Scene : Configure window behavior.
  • View Controller Scene : Where UI elements live

 

1. Pit 1: Delete ViewController custom Controller, run APP or blank window

By default, there are ViewController.h, ViewController.m, and Main.storyboard files. If you look at the storyboard view area, you will find that the program entry AppDelegate points to WindowController by default, and WindowController points to ViewController. The final display is the view on ViewController. You can refer to the above picture, or through Running the APP to view the layer can also be verified

The blogger habitually deleted the ViewController file, then created a custom MainViewController by himself, and added some UI on it, and found that the previous blank window was still displayed when starting the APP. Although the ViewController file was deleted, it actually pointed to ViewController, so We need to change the orientation, as follows:
through the Identity option in the rightmost inspector panel area; Identity: If the control uses a custom class, it needs to be selected from the Class drop-down list

Select the custom MainViewController, then the UI of Main.storyboard is associated with the controller, and the view on MainViewController is displayed when it is running

 

2. Pit 2: Window in Main.storyboard cannot be associated with AppDelegate

I want to associate the main Window in Main.storyboard in AppDelegate, but I can’t find it. I check the storyboard area view and find that its window points to WindowController.

Pointing to can also see its associated class

 

3. Pit 3: Delete Main.storyboard and use the custom created XIB file

After deleting the Main.storyboard file, the operation directly reported an error 'Could not find a storyboard named 'Main' in bundle NSBundle; had to restore it back

 

Because the main interface of the program startup view is specified in the project as Main.storyboard; we can customize a MainMenu.xib file, and specify MainMenu.xib in General----Deployment Info----Main Interface; at this time, it is running You will find that the main interface of the program startup is the UI on MainMenu.xib

At this time, we can associate Window with AppDelegate; its interface is not so rich compared with Main.storyboard, so we simply check its own interface properties

At this time, whether we use Main.storyboard or a custom MainMenu.xib file to load the main interface is all right

Initialize the interface through Main.storyboard and load the main interface

Initialize the interface by customizing the MainMenu.xib file and load the main interface

 

In summary, some differences between MacOS development and iOS development are also summarized:

For specific differences, please refer to the blog post Differences between MacOS-MAC development and IOS development (pure code)

The commonly used controller for MacOS development is NSWindowController instead of ViewController in iOS, because most mac apps have multiple windows, while iOS apps have only one main window. In most cases, we operate in ViewController to display different The page, and most of the UI controls start with UI, while MacOS development is just the opposite. Basically, the UI controls start with NS. The xib of each NSWindowController contains an NSWindow and an NSView.

Due to the lack of learning materials for MacOS development, looking at many demos downloaded from the Internet, it is found that most of them use xib, and relatively few use storyboard, except for a small part of pure code handwriting!

The coordinate system developed by MacOS is also different from the coordinate system developed by iOS. The origin of the coordinate system developed by MacOS is in the lower left corner of the XY coordinate system, while the origin of the coordinate system developed by iOS starts from the upper left corner of the XY coordinate system. Of course, if you are not used to it, you can forcibly modify its coordinate system, such as: by overriding the isFlipped method of the view, the origin of the view coordinate system is forced to be the upper left corner.

The method name is: - (BOOL)isFlipped{return YES;} Generally declare the position of a control in the window with NSMakeRect(0, 0, 110, 110) or CGRectMake(0, 0, 110, 110);

In MacOS development, the view itself does not provide properties such as background color, border, and rounded corners. But you can use the layer property to control these effects, you must set its property wantsLayer to YES before using these properties. This is much more convenient than developing without iOS. Of course, these effects can also be achieved by redrawing - (void)drawRect:(NSRect)dirtyRect

In the development of MacOS, NSWindow is generally used as the window. If you want to realize jumping and displaying another window, you need to close the current window and then display the next window.

 

Guess you like

Origin blog.csdn.net/MinggeQingchun/article/details/116273771