MonoTouch is a cross-platform solution for iOS development using the C # language. It supports iPhone/iPad development and currently supports the latest iOS 6 version.
Official Address: http://xamarin.com/
Github Sample: https://github.com/xamarin/monotouch-samples
App: http://xamarin.com/apps/all
MonoTouch can use the C # language for iOS development, which means that it can be easily used for iOS development as a. Net programmer. Of course, understanding the objective-c syntax is also helpful for your iOS learning.
This article mainly teaches you how to use MonoTouch to bind the native Obj-C static library and directly call the methods in the static library for implementation.
First, use xcode to create a native static Library:
Two files added to the MBProgressHUD Project (MBProgressHUD is a framework waiting to load special effects, github: https://github.com/jdg/MBProgressHUD)
Then compile it. By default, you can find the BindingLibraryl application in/Library/Developer/Xcode/DerivedData, libBindingLibrary.
Next, open MonoDevelop, create a BindingLibrary solution, and add a BindingLibrarySDK project:
Select "MonoTouch Binding Project", which is used to bind a native library.
Then, add libBindingLibrary. a to the project, and you will find that in the project, it will automatically generate a file called libBindingLibrary. linkwith. cs:
Using System;
Using MonoTouch. ObjCRuntime;
[Assembly: LinkWith ("libBindingLibrary. a", LinkTarget. Simulator, ForceLoad = true)]
In this way, the libBindingLibrary. a static library is imported, which is similar to importing non-C # (such as C ++) dynamic library by DllImport.
You can find that two files are automatically generated in the project, ApiDefinition. cs and StructsAndEnums. cs files. ApiDefintion. cs is mainly used as the ing of native class definitions.
For more information, see Binding Objective-C Types.
Write the code here:
[BaseType (typeof (UIView)]
Interface MBProgressHUD
{
[Static, Export ("showHUDAddedTo: animated:")]
MBProgressHUD ShowHUDAddedTo (UIView view, bool animated );
[Export ("show:")]
Void Show (bool animated );
[Export ("hide:")]
Void Hide (bool animated );
[Export ("hide: afterDelay:")]
Void Hide (bool animated, double delay );
[Export ("labelText", ArgumentSemantic. Copy)]
String LabelText {get; set ;}
The corresponding native interfaces include:
+ (MBProgressHUD *) showHUDAddedTo :( UIView *) view animated :( BOOL) animated;
-(Void) show :( BOOL) animated;
-(Void) hide :( BOOL) animated;
-(Void) hide :( BOOL) animated afterDelay :( NSTimeInterval) delay;
@ Property (copy) NSString * labelText;
In this example, BaseType indicates that its inheritance type is UIView, and the base class corresponding to the native MBProgressHUD class is UIView;
Export is the name of the imported method. Static indicates that it is used as a Static method;
ArgumentSematic is its parameter type: Copy;
Then you can directly compile BindingLibrarySDK to generate a BindingLibrarySDK. dll
Now, create a new iOS project:
Here, I select an iphone application with a Single Window.
Reference BindingLibrarySDK. dll:
Next, I designed a button control in the xib file to generate the code for clicking the event:
In the Click Event code, implement:
Partial void showWindow (MonoTouch. Foundation. NSObject sender)
{
BindingLibrarySDK. MBProgressHUD hud = BindingLibrarySDK. MBProgressHUD. ShowHUDAddedTo (this. View, true );
Hud. Show (true );
Hud. LabelText =;
Hud. Hide (true, 1); // hide hud after 1 s
}
Finally, run the program to view the effect:
Click the button to display the loading effect box. After one second, the effect box disappears.
In this way, MonoTouch is used to bind the native Obj-C static library and call related native methods.
Example: https://github.com/sunleepy/monotouch_binding