RAJESH
Am looking forward to new technology
Monday, June 23, 2014
Differences between strong and weak in objective-c
A strong reference (which you will use in most cases) means that you want to "own" the object you are referencing with this property/variable. The compiler will take care that any object that you assign to this property will not be destroyed as long as you (or any other object) points to it with a strong reference. Only once you set the property to
nil
will the object get destroyed (unless one or more other objects also hold a strong reference to it).
In contrast, with a weak reference you signify that you don't want to have control over the object's lifetime. The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to
nil
. The most frequent use cases of weak references in iOS are:- delegate properties, which are often referenced weakly to avoid retain cycles, and
- subviews/controls of a view controller's main view because those views are already strongly held by the main view.
atomic vs. nonatomic refers to the thread safety of the getter and setter methods that the compiler synthesizes for the property. atomic (the default) tells the compiler to make the accessor methods thread-safe (by adding a lock before an ivar is accessed) and nonatomic does the opposite. The advantage of nonatomic is slightly higher performance. On iOS, Apple uses nonatomic for almost all their properties so the general advice is for you to do the same.
Monday, February 17, 2014
Push and Pop VC based on Sting Selection
Push to View Controller based on String
A ===>VC
.M File
-(void) push
{
VCName *vcobj=[[VCName alloc]initWithNibName:@"VCName" bundle:nil];
[vcobj setPageString:@"SelectVC"];
[self.navigationController vcobj animated:YES];
// Example
ArriveTicketViewController *arrivelView=[[ArriveTicketViewController alloc]initWithNibName:@"ArriveTicketViewController" bundle:nil];
[arrivelView setPageString:@"SelectVC"];
[self.navigationController pushViewController:arrivelView animated:YES];
}
B ====> VC
.h File
{
NSString *pageString;
}
@property(nonatomic,strong) NSString *pageString;
.M File
@synthesize pageString;
if([self.pageString isEqualToString:@“SelectVC”])
{
// perform action
}
Poping the ViewController
-(void)PopToVC
{
for (UIViewController *viewcontroller in [self.navigationController viewControllers])
{
if ([viewcontroller isKindOfClass:[VCName class]])
{
VCNameobj =(VCName *)viewcontroller;
VCNameobj.pageString=@"SelectVC";
[self.navigationController popToViewController:viewcontroller animated:YES];
}
}
}
Sunday, February 16, 2014
Getting Current Location
.h file
BOOL didFindLocation;
.m file
- (void)viewDidLoad
[self startFindLocation];
-(void)startFindLocation
{
self.didFindLocation=NO;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark Map Current Location=========================
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// UIAlertView *errorAlert = [[UIAlertView alloc]
// initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// [errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (!self.didFindLocation)
{
self.didFindLocation = YES;
[locationManager stopUpdatingLocation];
self.longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
self.latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSLog(@"strLongitude===%@",self.longitude);
NSLog(@"strLatitude===%@",self.latitude);
}
}
NULL Checking For Webservices
#pragma mark ======== Null Checking for web services ===============
+(id)checkForNull:(id)value
{
NSString *valueString = [NSString stringWithFormat:@"%@",value];
id objectString = @"";
if (![valueString isEqualToString:@"(null)"] && ![valueString isEqualToString:@"<null>"] && valueString.length != 0)
return value;
return objectString;
}
EX
[localTrendsData setPhone_number:[self checkForNull:[currentLocalTrends valueForKey:@"phone_number"]]];
[localTrendsData setUser_id:[self checkForNull:[currentLocalTrends valueForKey:@"user_id"]]];
[localTrendsData setCategoryNameArray:[self checkForNull:[currentLocalTrends valueForKey:@"categories"]]];
Tuesday, October 29, 2013
Category Concepts Using Portrait to Landscape ViewController Pushing
Considering:
View A: Portrait only - View B: Landscape only
I
couldn't do it in the navigation controller. Instead what I did was
to open a modal view from view A to view B and force a new navigation
controller into this view.
This
is working for me in iOS5+.
You
need to create a category for the navigation controller like this:
UINavigationController+Rotation_IOS.h
#import
<UIKit/UIKit.h>
@interface
UINavigationController (Rotation_IOS)
-
(BOOL)shouldAutorotate;
-
(NSUInteger)supportedInterfaceOrientations;
-
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
@end
UINavigationController+Rotation_IOS.m
#import
"UINavigationController+Rotation_IOS.h"
@implementation
UINavigationController (Rotation_IOS)
-
(BOOL)shouldAutorotate
{
return
[self.topViewController
shouldAutorotate];
}
-
(NSUInteger)supportedInterfaceOrientations
{
return
[self.topViewController
supportedInterfaceOrientations];
}
-
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return
[self.topViewController
preferredInterfaceOrientationForPresentation];
}
@end
AppDelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return
UIInterfaceOrientationMaskAll;
}
VIEW A Portrait
-
(BOOL)shouldAutorotate
{
return
YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return
UIInterfaceOrientationMaskPortrait
| UIInterfaceOrientationMaskPortraitUpsideDown;
}
-
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return
UIInterfaceOrientationPortrait;
}
-
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return
(interfaceOrientation == UIInterfaceOrientationPortrait
|| interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
VIEW A to
VIEW B while Pushing
SignatureViewController
*signatureVC=[[SignatureViewController
alloc]
initWithNibName:@"SignatureViewController"
bundle:Nil];
UINavigationController
*navigationController = [[UINavigationController
alloc]
initWithRootViewController:signatureVC];
[self
presentViewController:navigationController
animated:YES
completion:nil];
VIEW
B Landscape
#pragma
mark -
#pragma
mark InterfaceOrientationMethods
-
(BOOL)shouldAutorotate
{
return
YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return
UIInterfaceOrientationMaskLandscapeRight
| UIInterfaceOrientationMaskLandscapeLeft;
}
-
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return
UIInterfaceOrientationLandscapeRight;
}
-
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return
(interfaceOrientation == UIInterfaceOrientationLandscapeRight
|| interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Monday, August 19, 2013
Objective-C: Delegate Protocols
To create your own custom delegate protocol, modify the header file for the selected class to add the @protocol declaration, a delegate @property, and declare the methods that delegates can implement:
// MyViewController.h: #import <UIKit/UIKit.h> @protocol MyProtocolName @interface MyViewController: UIViewController @property (nonatomic, weak) id<MyProtocolName> delegate; @end @protocol MyProtocolName <NSObject> @required -(void)requiredDelegateMethod; @optional -(void)optionalDelegateMethodOne; -(void)optionalDelegateMethodTwo:(NSString *)withArgument; @end // end of delegate protocol
In the implementation file, anytime you want to call a delegate method, first check to see if the delegate is set, and if it responds to the selector. Then call the method:
if (self.delegate && [self.delegate respondsToSelector:@selector(optionalDelegateMethodOne)]) { [self.delegate optionalDelegateMethodOne]; }
Now for classes you want to conform to your new protocol, include the header file and delegate protocol in the @interface:
#include "MyViewController.h" // needed to include the @delegate protocol info @interface FavoritesViewController : UIViewController <MyProtocolName>
Any required delegate methods must then be implemented in your class's @implementation file.
Subscribe to:
Posts (Atom)