- (BOOL)prefersStatusBarHidden
{
return YES;
}
Write above method on every UIViewController class which has a UIView to show without status bar.
Wednesday, July 23, 2014
Tuesday, June 10, 2014
A Code Snippet For Converting NSDate To NSString With NSDateFormatter
When working on programs that manipulate Dates sometimes those days need to be store & printed in particular format. iOS has given NSDate for date and time related data handling and NSDateFormatter can be used to give a date of NSDate object a visual form.
Code
Current Date and Time is obtained by init method of NSDate.
NSDate *date = [[NSDate alloc] init];
Create a NSDateFormatter for set the date format that is needed to print and get a NSString object from the date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
Use setDateFormat of NSDateFormatter to get a date format that is needed to display. This method get the required date format by using NSString parameter and returns nothing. The method has been called on the object dateFormatter.
[dateFormatter setDateFormat:@"dd-MM-YYYY HH:mm:ss"];
Method signature for setDateFormat
- (void)setDateFormat:(NSString *)string
NSString *dateString = [dateFormatter stringFromDate:date];
Invoke stringFromDate on dateFormatter object to get the the date value of NSDate object in specified date format. This method consumes previously created date object and assign a NSString represent of the date on date object to dateString.
Method signature for stringFromDate
- (NSString *)stringFromDate:(NSDate *)date
Print the dateString using NSLog();
NSLog(@"The Date: %@", dateString);
Complete Code
NSDate *date = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-YYYY HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"The Date: %@", dateString);
CodeProject
Monday, April 7, 2014
isDescendantOfView: To check whether UIView is a subview of another UIView in iOS
Sometimes it's needed to check whether a UIView is a subview of another view. A typical use case is show and hide UIViews with button click events. it's better if the code check whether a UIView is a sub view of another view before the view is removed from the super view. UIView class have method "isDescendantOfView" [1] it returns "YES" if the specified UIView is a subview of another UIView if it's not it returns "NO".
In example code below, "I m in" is printed if the theSubView is a sub view of the UIView self.view otherwise it prints "I m out".
if ([theSubView isDescendantOfView:self.view]) {
NSLog(@"I m in");
}else{
NSLog(@"I m out");
}
Reference:[1]. http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/isDescendantOfView:
Monday, March 24, 2014
Converting NSString to NSData and NSData to NSString
Description
Below code segment shows how to convert a NSString value to NSData and NSData value to NSString value. At the beginning, It create a NSString literal with the variable name "firstString" then value of "firstString" variable is taken as a way that can be assigned into NSData variable by using method [1]. The variable "firstString" is assigned with "nil" in order to show that the variable contains nothing. The value of the variable has printed to by using "NSLog". After the printing a NSString object is created with by using previously created NSData. Method [2] has been used. Then the "firstString" value is printed again with NSLog.
- - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
- - (instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
Code
NSString *firstString = @"This is the string that is wanted to convert";
NSData *dataFromString = [firstString dataUsingEncoding:NSUTF8StringEncoding];
firstString = nil;
NSLog(@"firstString (nil) = %@", firstString);
firstString = [[NSString alloc] initWithData:dataFromString encoding:NSUTF8StringEncoding];
NSLog(@"firstString (!nil) = %@", firstString);
Result
2014-03-24 17:10:11.367 TestProject[3048:60b] firstString (nil) = (null)
2014-03-24 17:10:11.369 TestProject[3048:60b] firstString (!nil) = This is the string that is wanted to convert
Subscribe to:
Posts (Atom)