Pages

Showing posts with label swift. Show all posts
Showing posts with label swift. Show all posts

Monday, March 4, 2013

Get Screen Size of iPhone/iPad

Here is a simple line to get the screen size in xCode.

UIScreen *screen = [UIScreen mainScreen];
CGRect fullScreenRect = screen.bounds;
//This will be used to get the width and height
fullScreenRect.size.widthfullScreenRect.size.height

And For Games, where Cocoas is using

CGSize winSize = [CCDirector sharedDirector].winSize;

NSLog(@" %f, %f", winSize.width, winSize.height);


Swift:

var screenUIScreen = UIScreen.mainScreen()
var fullScreenRect: CGRect = screen.bounds
//This will be used to get the width and height


fullScreenRect.size.widthfullScreenRect.size.height

Friday, January 18, 2013

Define Function in Xcode/Swift with multiple Arguments

Xcode:

Function should be in the format of
  selectorFragmentName:(ParameterType)parameterName

Example:
-(void)registerNewUser:(NSString *)userId andPassword:(NSString *)userPassword;

Swift:

private func registerNewUser(userID: String, password: String)-> Void

How to Hide Keyboard in xCode/swift

Objective C:
Just declare the function as mentioned below:

Declare function for TextHolder in "Did End Exit" event as below: in (.h) file

- (IBAction)editingCompleted:(id)sender;


now define function in (.m) file as below:


- (IBAction)editingCompleted:(id)sender {
    [sender resignFirstResponder]; // Here sender is the TextHolder, which text you can print also as sender.text
}


Always use resignFirstResponder with TextHolder, for which keyboard needs to hide.

Swift:
Declare function for TextHolder for "Did End On Exit"


@IBAction func editingCompleted(sender: UITextField) {
        sender.resignFirstResponder()

 }

Friday, January 11, 2013

How to print String in Xcode/Swift


To Print String in xCode:

I am reading a TextField String from xib. (labelName is the name of Label);

Objective C:


NSString *userId = [[NSString alloc] initWithString:labelName.text];
printf("String is %s", [userId UTF8String]);
[userId release];

Swift:

var userId = labelName.text
print(userId?.utf8)