Friday, 26 February 2016

UIAlertController in Swift

A UIAlertController object displays an alert message. This class replaces the UIActionSheet and UIAlertView classes.
In addition to displaying a message, associate actions with alert controller to give a way to respond.
Click Here for more details.

//Display UIAlertView
func displayAlert(){
        let alertController = UIAlertController(title: "Alert Message", message: "Welcome to you", preferredStyle: .Alert)
        alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        presentViewController(alertController, animated: true, completion: nil)
    }


//Add More action with UIAlertController
func alertOkButtonAction(actionTarget: UIAlertAction){
        print("Ok Action Call")
    }

 //create a action
let OkAction = UIAlertAction(title: "Ok", style: .Default, handler: alertOkButtonAction)
alertController.addAction(OkAction)


//Display alert with  UITextField
alertController.addTextFieldWithConfigurationHandler { (nameTxtFld) -> Void in
           nameTxtFld.textColor = UIColor.blueColor()
        }

//We can also add action on UItextField
 func textChange(txtFld:UITextField){
        let str = txtFld.text! as NSString
        print(str)
       
    }

//Add action on UITextField
alertController.addTextFieldWithConfigurationHandler { (nameTxtFld) -> Void in
           nameTxtFld.textColor = UIColor.blueColor()

           nameTxtFld.addTarget(self, action: "textChange:", forControlEvents: .EditingChanged)
        }

Friday, 19 February 2016

Create a Simple Drawing App

A UIBezierPath object combines the geometry of a path with attributes that describe the path during rendering. We can set the geometry and attributes separately and can change them independent of one another. Once we have the object configured the way you want it, you can tell it to draw itself in the current context. Because the creation, configuration, and rendering process are all distinct steps, Bezier path objects can be reused easily in  code. We can even use the same object to render the same shape multiple times, perhaps changing the rendering options between successive drawing calls. 
For more details about UIBezierPath Click Here.

Now, we are going to create a simple drawing app using UIBezierpath.

First, create a  subclass of UIView Class.
Suppose name is SketchView
 Inside SketchView.h
@interface SketchView : UIView
{
    UIBezierPath *scriblePath;
}


Add following code in SketchView.m

@implementation SketchView

-(instancetype) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        scriblePath = [[UIBezierPath alloc] init];
        scriblePath.lineCapStyle=kCGLineCapRound;
        scriblePath.miterLimit=0;
        scriblePath.lineWidth = 2; //Set Stroke with
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [[UIColor blackColor] setStroke];
    [scriblePath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}

#pragma mark -
-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
     CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
    [scriblePath moveToPoint:touchPoint];
}
-(void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
     CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
    [scriblePath addLineToPoint:touchPoint];
    [self setNeedsDisplay];
}

@end

 

Friday, 12 February 2016

Add PCH file in Xcode6


In the C and C++ programming languages, a header file is a file whose text may be automatically included in another source file by the C preprocessor, usually specified by the use of compiler directives in the source file.

Prefix.pch is a precompiled header file. Precompiled headers were invented to make compiling faster. Rather than parsing the same header files over and over, these files get parsed once, ahead of time.
Xcode 5 and older version of Xcode by default add PCH file with project, but Xcode 6 doesn't include PCH file automatocally.
We can add PCH file in Project.
Here steps to add PCH file in Project:

1) Create new file by selecting File > New File > iOS > Other > PCH File > Next.
2) Give name to newly created file.(Ex- PrefixHeader)
3) Select Project > Targets > Build Setting.
4) Search for 'Prefix header'.
5) Under "Apple LLVM 7.0" you will get the Prefix Header Key.

 6) Type file directory. (Ex- ProjectFolder/ProjeectName-Prefix.pch).
7) Clean project.
8) Build project.