Implement a window program code implementation in Xcode

Source: Internet
Author: User

InXcodeTo implementWindowProgram code implementation is the content to be introduced in this article. It mainly explains how to implementWindowProgram, understandXCodeAndInterface builderWhat has been done for our development work.

CocoaThe coordinate system is a bit different from the habit. It is strange to get used to the Quadrant coordinate system and use the first quadrant. This article mainly explains how to implement a window program by writing code. Of course, this program is very simple, but it demonstrates some things we should know, therefore, we should be able to better understandXCodeAndInterface builderWhat has been done for our development work.

Majority of explanationsCocoaProgramming books will be usedXcodeAndInterface builderThe combination of these two tools is developmentCocoaThe best choice for applications, but many details are often hidden in orderCocoaHave a better understanding of the operation method, I decided to use code to implement someInterface BuilderTo study the functions of the Nib file.Cocoa.

Next let's take a look at the simplest HelloWin source code.

 
 
  1. #import <Cocoa/Cocoa.h> 
  2.  
  3. int main(int argc, char* argv[])  
  4. {  
  5.     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];  
  6.     NSApplication* app = [NSApplication sharedApplication];  
  7.  
  8.     //Create the main window  
  9.     NSRect rc = NSMakeRect(0, 0, 800, 600);  
  10.     NSUInteger uiStyle = NSTitledWindowMask | NSResizableWindowMask | NSClosableWindowMask;  
  11.     NSBackingStoreType backingStoreStyle = NSBackingStoreBuffered;  
  12.     NSWindow* win = [[NSWindow alloc] initWithContentRect:rc styleMask:uiStyle backing:backingStoreStyle defer:NO];  
  13.     [win setTitle:@"HelloWin Test"];  
  14.     [win makeKeyAndOrderFront:win];  
  15.     [win makeMainWindow];  
  16.  
  17.     //Start the event loop by calling NSApp run  
  18.     [NSApp run];  
  19.     [pool drain];  
  20.     return 0;  

The compilation command is as follows:

 
 
  1. gcc HelloWin.m -framework Cocoa -o HelloWin 

All Cocoa programs connect to the Cocoa Framework. The Cocoa Framework automatically references other required frameworks, such as the Foundation framework. The program generated by this compilation command is HelloWin. You can run the following command on the terminal to check the running status of the program.

 
 
  1. ./HelloWin 

Window display:

You will find that this program has no icon on the Dock and there is no menu on the menu bar. Although you can close this window, you can see that the application has not exited at the terminal prompt, press ctrl + C on the terminal to end the application.

Next, let's take a look at this simple program. First, we imported the Cocoa Framework and created an instance of the NSAID utoreleasepool, next, we get an NSApplication object through the class method sharedApplication of NSApplication and assign this object to an instance of the NSApplication class. In fact, this assignment is not required, when you call sharedApplication, Cocoa sets another global NSApplication object instance, NSApp. Therefore, you can see that the handler that calls the run method is an NSApp, instead of an app. So you can simply call

 
 
  1. [NSApplication sharedApplication]; 

You can use NSApp directly when you need an NSApplication object in the future.

After the NSApp is obtained, the program starts to create a window, because this is also the main window mainWindow of the Program), so when creating this window, styleMask for windows must also meet certain conditions. If a window is to be a main window, it must have a title (NSTitleWidowMask) or an NSResizableWindowMask attribute ). In addition, if you want this window to be closed, NSClosableWindowMask is also required. Otherwise, although this window can be displayed, you will find that the close button on the title bar is gray.

After the initialization window, setTitle is used to set the title of the window. The call to makeKeyAndOrderFront: is critical, because a window to become mainWindow must be visible. After this method is called, we can use the makeMainWindow method to make this window the main window of the program.

With masterWindowThen, we can call the run method of NSApplication to start the message loop of the program. Then the application starts to run and can accept various applications orWindow.

Note that the initial Rect start point is 0, 0, but is displayed in the lower left corner of the screen, this problem occurs because the default Coordinate System on Mac is different from that on Windows. The origin of the coordinate system on Mac is in the lower left corner of the screen, so the screen is out of the first quadrant, this is consistent with the knowledge we learned in middle school and will be discussed later.

Summary: InXcodeTo implementWindowI hope this article will be helpful to you.

Original post address http://www.cocoachina.com/bbs/read.php? Tid-23.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.