Monday 11 January 2016

How to create a UIButton in Xcode

Xcode:

Xcode is Apple's integrated development environment (IDE) that you use to build apps for Apple products such as the iPad, iPhone, and Mac. Xcode provides tools to manage your entire development workflow—from creating your app, to testing, optimizing, and submitting it to the App Store.

Xcode includes everything you need to create amazing apps for iPhone, iPad, Mac, and Apple Watch. The Swift programming language has been updated and is now faster than ever, with great features that make your code even easier to read and write.

Xcode provides several technologies to help you develop an internationalized app. Xcode separates user-facing text from your views and layout so user-facing text can be easily translated independent of your Xcode project. Xcode also provides tools to maintain this separation when you change the user interface. In addition, you may have different sets of other types of resource files for each language you support.

Xcode supports C, C++, Objective-C, Objective-C++, Java, AppleScript, Python, Ruby, Rez, and Swift source code with a variety of programming models, including but not limited to Cocoa, Carbon, and Java. Third parties have added support for GNU Pascal, Free Pascal, Ada, C#, Perl, and D.

Objective-C:

Objective-C is general-purpose language that is developed on top of C Programming language by adding features of Small Talk programming language making it an object-oriented language. It is primarily used in developing iOS and Mac OS X operating systems as well as its applications.

Swift:

Swift is a new programming language developed by Apple Inc for iOS and OS X development. Swift adopts the best of C and Objective-C, without the constraints of C compatibility. Swift uses the same runtime as the existing Obj-C system on Mac OS and iOS which makes Swift programs run on many existing iOS 6 and OS X 10.8 platforms.

Mostly developers prefer Objective c and Swift languages to code for iOS , Mac OSX, iWatch, iPod.

About iOS Cocoa framework:

Cocoa is a set of frameworks that Apple built for developers to use when building Mac and iOS apps. The Cocoa frameworks are written in Objective-C and Objective-C is the preferred language used to access the Cocoa frameworks when developing Mac and iOS applications.

How to create a UIButton programmatically:

In Objective-C:

   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"OK" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);

    [self.view addSubview:button];

-(void) buttonClicked:(UIButton*)sender
 {
NSLog(@“Button clicked”);
 }

In Swift:

override func viewDidLoad() {
super.viewDidLoad()  

    let button = UIButton()
    button.frame = CGRectMake(10, 10, 50, 50)
    button.setTitle(“OK”, forState: .Normal)
    button.addTarget(self, action: "onClick:”, forControlEvents: .TouchUpInside) 
    self.view.addSubview(button) 

}

func onClick(sender: UIButton!) 
{
   print(“Button clicked”)
}

UIButton inherits from—>NSObject—>UIResponder—>UIView—>UIControl—>UIButton

To know more about UIButton class click here.