Saturday, September 28, 2013

C++ beginers code for Objective-C Developers using XCode

Problem 1: Using CPP code show the use of If…Then…Else statement-using user input values.

Code

//
//  main.cpp
//  Hello World CPP
//
//  Created by Bishal Ghimire on 9/19/13.
//  Copyright (c) 2013 Bishal Ghimire. All rights reserved.
//

#include <iostream>

using namespace std;


/**
   IF then else function for using user input values
*/

int main(int argc, const char * argv[]) {

    int inputAge;
    std::cout << "Input You Age, !\n";
    cin >> inputAge;
   
   
    if (inputAge >= 18) {
        cout << "You can Vote ! \n";
    }
    else if (inputAge < 18) {
        cout << "You can NOT vote ! \n";
    }
    return 0;
}

Output

$ g++ main.cpp
$ ./a.out
Input You Age, !
 29
You can Vote !

Problem 2: Using functions show how you can swipe variables using call by reference.

Code

/**
    Function to swipe two variables using third temp value
 */
void swap(int *u, int *v) {
    int temp;
    temp = *u;
    *u = *v;
    *v = temp;
    return;
}

int main(int argc, const char * argv[]) {
       
    int firstValue;
    cout<<"Input First Value \n";
    cin >> firstValue;
   
    int secondValue;
    cout<<"Input Second Value \n";
    cin >> secondValue;
   
    swap(firstValue, secondValue);
   
    cout<<"First Value and Second Value after they are Swaped \n";
    cout << "1st = " << firstValue << "\n2nd = " << secondValue;
    cout<<"\n";
    return 0;
}

Output

$ g++ main.cpp
$ ./a.out
Input First Value
12
Input Second Value
34
First Value and Second Value after they are Swaped
1st = 34
2nd = 12

Problem 3: Demonstrate the use of functional overloading in CPP (C++)

 Code

/**
  functionOverLoading.cpp
*/

#include "functionOverLoading.h"

using namespace std;

#define pi 3.141

/**
 Function overloading
 */
class fxn {
public:
    void area(int);              // circle
    void area(int, int);         // rectangle
    void area(int, int, int);    // triangle
};

void fxn::area(int a) {
    cout<<"Area of Circle: "<<pi*a*a;
    cout<<"\n";
}

void fxn::area(int a, int b) {
    cout<<"Area of rectangle: "<<a*b;
    cout<<"\n";
}

void fxn::area(int a, int b, int c) {
    // cout<<"Area of triangle: "<<a*b*c;
    cout<<"\n";
}

typedef enum ChoiceType
{
    choiceCircle    = 1,
    choiceRectangle,
    choiceTriangle,
    choiceExit
} ChoiceType;


int main(int argc, const char * argv[]) {
   
    int choice;
    int radius;
    int length;
    int breath;
    int sideA, sideB, sideC;
    fxn object;
   
    cout<<"\n\t\tFunction Overloading";
    cout<<"\n 1. Area of Circle \n 2. Area of Rectangle \n 3. Area of Triangle \n 4. Exit \n ";
    cout<<"Enter your Choice:";
    cin>>choice;
   
    switch (choice) {

        case choiceCircle:
            cout<<"Enter Radious of the Circle:";
            cin>>radius;
            object.area(radius);
            break;

        case choiceRectangle:
            cout<<"Enter Sides of Rectangle:";
            cin>>length>>breath;
            object.area(length, breath);
            break;

        case choiceTriangle:
            cout<<"Enter Sides of triangle:";
            cin>>sideA>>sideB>>sideC;
            object.area(sideA, sideB, sideC);
            break;

        case choiceExit:
            exit(0);
            break;

        default:
            break;
    }
    return 0;
}

Output 

$g++ functionOverLoading.cpp –o functionOverLoading
$ ./functionOverLoading

Function Overloading
 1. Area of Circle
 2. Area of Rectangle
 3. Area of Triangle
 4. Exit
 Enter your Choice:1
Enter Radious of the Circle:10
Area of Circle: 314.1


$ ./functionOverLoading
            Function Overloading
 1. Area of Circle
 2. Area of Rectangle
 3. Area of Triangle
 4. Exit
 Enter your Choice:2
Enter Sides of Rectangle:10 12
Area of rectangle: 120


Wednesday, September 25, 2013

Avoid Keyboard in UITableView Cell

The best solution to avoid keyboard in iOS in table view are to use framework called TPKeyboardAvoid https://github.com/michaeltyson/TPKeyboardAvoiding

But, if you need some manual solution here are few codes

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

}

-(void) viewDidDisappear:(BOOL)animated  {
    [super viewDidDisappear:YES];
    blogParser.delegate = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

-(CGFloat)getLabelHeightForIndex:(NSInteger)index
{
    CGSize maximumSize = CGSizeMake(230, 10000);
    Blog_Comment *comment = comments[index];
    
    CGSize labelHeighSize = [comment.comment sizeWithFont: [UIFont fontWithName:@"Helvetica" size:12.0f]
                                     constrainedToSize:maximumSize
                                         lineBreakMode:NSLineBreakByCharWrapping];
    return labelHeighSize.height;
    
}

#pragma mark - Notification 

-(void) moveTableViewBottomRowUp:(NSNotification *)note {
    NSDictionary *userInfo = [note userInfo];
    CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    kKeyboardHeight = kbSize.height;
    
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0);
        [_tableView setContentInset:edgeInsets];
        [_tableView setScrollIndicatorInsets:edgeInsets];
//        [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[_tableView numberOfRowsInSection:0]-1 inSection:0]
//                          atScrollPosition:UITableViewScrollPositionTop
//                                  animated:YES];
    }];
}

-(void) moveTableViewBottom2Normal:(NSNotification *)note {
    NSDictionary *userInfo = [note userInfo];
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
        [_tableView setContentInset:edgeInsets];
        [_tableView setScrollIndicatorInsets:edgeInsets];
    }];
}

-(void) assignKeyBoardHeigh:(NSNotification *) note {
    NSDictionary *userInfo = [note userInfo];
//    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    kKeyboardHeight = kbSize.height;
}

-(void) keyboardWillShow:(NSNotification *)note {
    [self assignKeyBoardHeigh:note];
    [self moveTableViewBottomRowUp:note];
    [self moveTextFieldUp];
}

-(void) keyboardWillHide:(NSNotification *)note {
//    [self moveTableViewBottom2Normal:note];
    [self moveTexyFieldDown];
}


-(void) moveTextFieldUp {
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:0.2];
    CGRect frame = viewComment.frame;
    frame.origin.y = self.view.bounds.size.height - kKeyboardHeight - frame.size.height;
    viewComment.frame = frame;
    // DLog(@"%f", viewComment.frame.origin.y);
    [UIView commitAnimations];
}

-(void) moveTexyFieldDown {
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:0.2];
    CGRect frame = viewComment.frame;
    frame.origin.y = self.view.bounds.size.height - frame.size.height;
    viewComment.frame = frame;
    
    // DLog(@"%f", viewComment.frame.origin.y);
    [UIView commitAnimations];
}



http://iphoneimei.info/