iphone FullScreen navigation bar displacement - iphone

I implemented an app Images in full screen are displayed fine.
After few seconds the navigation bar and status bar are hidden, now if i close the app and again open it, the navigation bar is displaced at the top of the screen where status bar overlaps on navigation bar
I guess i have to change something about the CGRect frame
Please help me
#import "KTPhotoScrollViewController.h"
#import "KTPhotoBrowserDataSource.h"
#import "KTPhotoBrowserGlobal.h"
#import "KTPhotoView.h"
const CGFloat ktkDefaultPortraitToolbarHeight = 44;
const CGFloat ktkDefaultLandscapeToolbarHeight = 33;
const CGFloat ktkDefaultToolbarHeight = 44;
#define BUTTON_DELETEPHOTO 0
#define BUTTON_CANCEL 1
#interface KTPhotoScrollViewController (KTPrivate)
- (void)setCurrentIndex:(NSInteger)newIndex;
- (void)toggleChrome:(BOOL)hide;
- (void)startChromeDisplayTimer;
- (void)cancelChromeDisplayTimer;
- (void)hideChrome;
- (void)showChrome;
- (void)swapCurrentAndNextPhotos;
- (void)nextPhoto;
- (void)previousPhoto;
- (void)toggleNavButtons;
- (CGRect)frameForPagingScrollView;
- (CGRect)frameForPageAtIndex:(NSUInteger)index;
- (void)loadPhoto:(NSInteger)index;
- (void)unloadPhoto:(NSInteger)index;
- (void)trashPhoto;
- (void)exportPhoto;
#end
#implementation KTPhotoScrollViewController
#synthesize statusBarStyle = statusBarStyle_;
#synthesize statusbarHidden = statusbarHidden_;
#synthesize my_img, imgURL;
- (void)dealloc
{
[nextButton_ release], nextButton_ = nil;
[previousButton_ release], previousButton_ = nil;
[scrollView_ release], scrollView_ = nil;
[toolbar_ release], toolbar_ = nil;
[photoViews_ release], photoViews_ = nil;
[dataSource_ release], dataSource_ = nil;
[super dealloc];
}
- (id)initWithDataSource:(id <KTPhotoBrowserDataSource>)dataSource andStartWithPhotoAtIndex:(NSUInteger)index
{
if (self = [super init]) {
startWithIndex_ = index;
dataSource_ = [dataSource retain];
// Make sure to set wantsFullScreenLayout or the photo
// will not display behind the status bar.
[self setWantsFullScreenLayout:YES];
BOOL isStatusbarHidden = [[UIApplication sharedApplication] isStatusBarHidden];
[self setStatusbarHidden:isStatusbarHidden];
self.hidesBottomBarWhenPushed = YES;
}
return self;
}
- (void)loadView
{
[super loadView];
CGRect scrollFrame = [self frameForPagingScrollView];
UIScrollView *newView = [[UIScrollView alloc] initWithFrame:scrollFrame];
[newView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[newView setDelegate:self];
UIColor *backgroundColor = [dataSource_ respondsToSelector:#selector(imageBackgroundColor)] ?
[dataSource_ imageBackgroundColor] : [UIColor blackColor];
[newView setBackgroundColor:backgroundColor];
[newView setAutoresizesSubviews:YES];
[newView setPagingEnabled:YES];
[newView setShowsVerticalScrollIndicator:NO];
[newView setShowsHorizontalScrollIndicator:NO];
[[self view] addSubview:newView];
scrollView_ = [newView retain];
[newView release];
nextButton_ = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"nextIcon.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(nextPhoto)];
previousButton_ = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"previousIcon.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(previousPhoto)];
UIBarButtonItem *msgButton = nil;
UIBarButtonItem *exportButton = nil;
exportButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(exportPhoto)];
msgButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self
action:#selector(msgPhoto)];
// UIImage *image = [UIImage imageNamed:#"Icon-Small"];
// UIButton *myMuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
// myMuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
// [myMuteButton setImage:image forState:UIControlStateNormal];
// [myMuteButton addTarget:self action:#selector(trashPhoto) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem *myMuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myMuteButton];
UIBarItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSMutableArray *toolbarItems = [[NSMutableArray alloc] initWithCapacity:7];
if (exportButton) [toolbarItems addObject:exportButton];
[toolbarItems addObject:space];
[toolbarItems addObject:previousButton_];
[toolbarItems addObject:space];
[toolbarItems addObject:nextButton_];
[toolbarItems addObject:space];
if (msgButton) [toolbarItems addObject:msgButton];
// [toolbarItems addObject:myMuteBarButtonItem];
// [myMuteBarButtonItem release];
CGRect screenFrame = [[UIScreen mainScreen] bounds];
CGRect toolbarFrame = CGRectMake(0,
screenFrame.size.height - ktkDefaultToolbarHeight,
screenFrame.size.width,
ktkDefaultToolbarHeight);
toolbar_ = [[UIToolbar alloc] initWithFrame:toolbarFrame];
[toolbar_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin];
[toolbar_ setBarStyle:UIBarStyleBlackTranslucent];
[toolbar_ setItems:toolbarItems];
[[self view] addSubview:toolbar_];
if (msgButton) [msgButton release];
if (exportButton) [exportButton release];
[toolbarItems release];
[space release];
}
- (void) ShowAlert:(NSString*)title MyMsg:(NSString*)msg{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert autorelease];
}
- (void)setTitleWithCurrentPhotoIndex
{
NSString *formatString = NSLocalizedString(#"%1$i of %2$i", #"Picture X out of Y total.");
NSString *title = [NSString stringWithFormat:formatString, currentIndex_ + 1, photoCount_, nil];
[self setTitle:title];
}
- (void)scrollToIndex:(NSInteger)index
{
CGRect frame = scrollView_.frame;
frame.origin.x = frame.size.width * index;
frame.origin.y = 0;
[scrollView_ scrollRectToVisible:frame animated:NO];
}
- (void)setScrollViewContentSize
{
NSInteger pageCount = photoCount_;
if (pageCount == 0) {
pageCount = 1;
}
CGSize size = CGSizeMake(scrollView_.frame.size.width * pageCount,
scrollView_.frame.size.height / 2); // Cut in half to prevent horizontal scrolling.
[scrollView_ setContentSize:size];
}
- (void)viewDidLoad
{
[super viewDidLoad];
photoCount_ = [dataSource_ numberOfPhotos];
[self setScrollViewContentSize];
// Setup our photo view cache. We only keep 3 views in
// memory. NSNull is used as a placeholder for the other
// elements in the view cache array.
photoViews_ = [[NSMutableArray alloc] initWithCapacity:photoCount_];
for (int i=0; i < photoCount_; i++) {
[photoViews_ addObject:[NSNull null]];
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// The first time the view appears, store away the previous controller's values so we can reset on pop.
UINavigationBar *navbar = [[self navigationController] navigationBar];
if (!viewDidAppearOnce_) {
viewDidAppearOnce_ = YES;
navbarWasTranslucent_ = [navbar isTranslucent];
statusBarStyle_ = [[UIApplication sharedApplication] statusBarStyle];
}
// Then ensure translucency. Without it, the view will appear below rather than under it.
[navbar setTranslucent:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
// Set the scroll view's content size, auto-scroll to the stating photo,
// and setup the other display elements.
[self setScrollViewContentSize];
[self setCurrentIndex:startWithIndex_];
[self scrollToIndex:startWithIndex_];
[self setTitleWithCurrentPhotoIndex];
[self toggleNavButtons];
[self startChromeDisplayTimer];
}
- (void)viewWillDisappear:(BOOL)animated
{
// Reset nav bar translucency and status bar style to whatever it was before.
UINavigationBar *navbar = [[self navigationController] navigationBar];
[navbar setTranslucent:navbarWasTranslucent_];
[[UIApplication sharedApplication] setStatusBarStyle:statusBarStyle_ animated:YES];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[self cancelChromeDisplayTimer];
[super viewDidDisappear:animated];
}
- (void)deleteCurrentPhoto
{
if (dataSource_) {
// TODO: Animate the deletion of the current photo.
NSInteger photoIndexToDelete = currentIndex_;
[self unloadPhoto:photoIndexToDelete];
[dataSource_ deleteImageAtIndex:photoIndexToDelete];
photoCount_ -= 1;
if (photoCount_ == 0) {
[self showChrome];
[[self navigationController] popViewControllerAnimated:YES];
} else {
NSInteger nextIndex = photoIndexToDelete;
if (nextIndex == photoCount_) {
nextIndex -= 1;
}
[self setCurrentIndex:nextIndex];
[self setScrollViewContentSize];
}
}
}
- (void)toggleNavButtons
{
[previousButton_ setEnabled:(currentIndex_ > 0)];
[nextButton_ setEnabled:(currentIndex_ < photoCount_ - 1)];
}
#pragma mark -
#pragma mark Frame calculations
#define PADDING 20
- (CGRect)frameForPagingScrollView
{
CGRect frame = [[UIScreen mainScreen] bounds];
frame.origin.x -= PADDING;
frame.size.width += (2 * PADDING);
return frame;
}
- (CGRect)frameForPageAtIndex:(NSUInteger)index
{
CGRect bounds = [scrollView_ bounds];
CGRect pageFrame = bounds;
pageFrame.size.width -= (2 * PADDING);
pageFrame.origin.x = (bounds.size.width * index) + PADDING;
return pageFrame;
}
#pragma mark -
#pragma mark Photo (Page) Management
- (void)loadPhoto:(NSInteger)index
{
if (index < 0 || index >= photoCount_) {
return;
}
id currentPhotoView = [photoViews_ objectAtIndex:index];
if (NO == [currentPhotoView isKindOfClass:[KTPhotoView class]]) {
// Load the photo view.
CGRect frame = [self frameForPageAtIndex:index];
KTPhotoView *photoView = [[KTPhotoView alloc] initWithFrame:frame];
[photoView setScroller:self];
[photoView setIndex:index];
[photoView setBackgroundColor:[UIColor clearColor]];
// Set the photo image.
if (dataSource_) {
if ([dataSource_ respondsToSelector:#selector(imageAtIndex:photoView:)] == NO) {
UIImage *image = [dataSource_ imageAtIndex:index];
[photoView setImage:image];
} else {
[dataSource_ imageAtIndex:index photoView:photoView];
}
}
[scrollView_ addSubview:photoView];
[photoViews_ replaceObjectAtIndex:index withObject:photoView];
[photoView release];
} else {
// Turn off zooming.
[currentPhotoView turnOffZoom];
}
}
- (void)unloadPhoto:(NSInteger)index
{
if (index < 0 || index >= photoCount_) {
return;
}
id currentPhotoView = [photoViews_ objectAtIndex:index];
if ([currentPhotoView isKindOfClass:[KTPhotoView class]]) {
[currentPhotoView removeFromSuperview];
[photoViews_ replaceObjectAtIndex:index withObject:[NSNull null]];
}
}
- (void)setCurrentIndex:(NSInteger)newIndex
{
currentIndex_ = newIndex;
if(newIndex>=0){
myUrl = [dataSource_ imageURLAtIndex:currentIndex_ photoView:[photoViews_ objectAtIndex:currentIndex_]];
myDescr = [dataSource_ imageDESCRAtIndex:currentIndex_ photoView:[photoViews_ objectAtIndex:currentIndex_]];
img_Title =[dataSource_ imageimg_TitleAtIndex:currentIndex_ photoView:[photoViews_ objectAtIndex:currentIndex_]];
}
[self loadPhoto:currentIndex_];
[self loadPhoto:currentIndex_ + 1];
[self loadPhoto:currentIndex_ - 1];
[self unloadPhoto:currentIndex_ + 2];
[self unloadPhoto:currentIndex_ - 2];
[self setTitleWithCurrentPhotoIndex];
[self toggleNavButtons];
}
#pragma mark -
#pragma mark Rotation Magic
- (void)updateToolbarWithOrientation:(UIInterfaceOrientation)interfaceOrientation
{
CGRect toolbarFrame = toolbar_.frame;
if ((interfaceOrientation) == UIInterfaceOrientationPortrait || (interfaceOrientation) == UIInterfaceOrientationPortraitUpsideDown) {
toolbarFrame.size.height = ktkDefaultPortraitToolbarHeight;
} else {
toolbarFrame.size.height = ktkDefaultLandscapeToolbarHeight+1;
}
toolbarFrame.size.width = self.view.frame.size.width;
toolbarFrame.origin.y = self.view.frame.size.height - toolbarFrame.size.height;
toolbar_.frame = toolbarFrame;
}
- (void)layoutScrollViewSubviews
{
[self setScrollViewContentSize];
NSArray *subviews = [scrollView_ subviews];
for (KTPhotoView *photoView in subviews) {
CGPoint restorePoint = [photoView pointToCenterAfterRotation];
CGFloat restoreScale = [photoView scaleToRestoreAfterRotation];
[photoView setFrame:[self frameForPageAtIndex:[photoView index]]];
[photoView setMaxMinZoomScalesForCurrentBounds];
[photoView restoreCenterPoint:restorePoint scale:restoreScale];
}
// adjust contentOffset to preserve page location based on values collected prior to location
CGFloat pageWidth = scrollView_.bounds.size.width;
CGFloat newOffset = (firstVisiblePageIndexBeforeRotation_ * pageWidth) + (percentScrolledIntoFirstVisiblePage_ * pageWidth);
scrollView_.contentOffset = CGPointMake(newOffset, 0);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
// here, our pagingScrollView bounds have not yet been updated for the new interface orientation. So this is a good
// place to calculate the content offset that we will need in the new orientation
CGFloat offset = scrollView_.contentOffset.x;
CGFloat pageWidth = scrollView_.bounds.size.width;
if (offset >= 0) {
firstVisiblePageIndexBeforeRotation_ = floorf(offset / pageWidth);
percentScrolledIntoFirstVisiblePage_ = (offset - (firstVisiblePageIndexBeforeRotation_ * pageWidth)) / pageWidth;
} else {
firstVisiblePageIndexBeforeRotation_ = 0;
percentScrolledIntoFirstVisiblePage_ = offset / pageWidth;
}
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[self layoutScrollViewSubviews];
// Rotate the toolbar.
[self updateToolbarWithOrientation:toInterfaceOrientation];
// Adjust navigation bar if needed.
if (isChromeHidden_ && statusbarHidden_ == NO) {
UINavigationBar *navbar = [[self navigationController] navigationBar];
CGRect frame = [navbar frame];
frame.origin.y = 20;
[navbar setFrame:frame];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self startChromeDisplayTimer];
}
- (UIView *)rotatingFooterView
{
return toolbar_;
}
#pragma mark -
#pragma mark Chrome Helpers
- (void)toggleChromeDisplay
{
[self toggleChrome:!isChromeHidden_];
}
- (void)toggleChrome:(BOOL)hide
{
isChromeHidden_ = hide;
if (hide) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
}
if ( ! [self isStatusbarHidden] ) {
if ([[UIApplication sharedApplication] respondsToSelector:#selector(setStatusBarHidden:withAnimation:)]) {
[[UIApplication sharedApplication] setStatusBarHidden:hide withAnimation:NO];
} else { // Deprecated in iOS 3.2+.
id sharedApp = [UIApplication sharedApplication]; // Get around deprecation warnings.
[sharedApp setStatusBarHidden:hide animated:NO];
}
}
CGFloat alpha = hide ? 0.0 : 1.0;
// Must set the navigation bar's alpha, otherwise the photo
// view will be pushed until the navigation bar.
UINavigationBar *navbar = [[self navigationController] navigationBar];
[navbar setAlpha:alpha];
[toolbar_ setAlpha:alpha];
if (hide) {
[UIView commitAnimations];
}
if ( ! isChromeHidden_ ) {
[self startChromeDisplayTimer];
}
}
- (void)hideChrome
{
if (chromeHideTimer_ && [chromeHideTimer_ isValid]) {
[chromeHideTimer_ invalidate];
chromeHideTimer_ = nil;
}
[self toggleChrome:YES];
}
- (void)showChrome
{
[self toggleChrome:NO];
}
- (void)startChromeDisplayTimer
{
[self cancelChromeDisplayTimer];
chromeHideTimer_ = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(hideChrome)
userInfo:nil
repeats:NO];
}
- (void)cancelChromeDisplayTimer
{
if (chromeHideTimer_) {
[chromeHideTimer_ invalidate];
chromeHideTimer_ = nil;
}
}
#pragma mark -
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = scrollView.frame.size.width;
float fractionalPage = scrollView.contentOffset.x / pageWidth;
NSInteger page = floor(fractionalPage);
if (page != currentIndex_) {
[self setCurrentIndex:page];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self hideChrome];
}
#pragma mark -
#pragma mark Toolbar Actions
- (void)nextPhoto
{
[self scrollToIndex:currentIndex_ + 1];
[self startChromeDisplayTimer];
}
- (void)previousPhoto
{
[self scrollToIndex:currentIndex_ - 1];
[self startChromeDisplayTimer];
}
- (void)msgPhoto
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:img_Title message:myDescr delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSString *message;
NSString *title;
if (!error) {
title = #"Done";
message = #"image copied to your local gallery";
} else {
title = #"Error";
message = [error description];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
//save to gallery
UIImage *imageB = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: myUrl]]];
UIImageWriteToSavedPhotosAlbum(imageB, self, #selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
} else if (buttonIndex == 1) {
//email
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
mailComposer.toolbar.barStyle = UIBarStyleBlack;
mailComposer.title = #"Your title here";
[[mailComposer navigationBar] setTintColor:[UIColor colorWithRed:124.0/255 green:17.0/255 blue:92.0/255 alpha:1]];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setSubject:#"Look at a great image"];
[mailComposer setMessageBody:[NSString stringWithFormat:#"%#",myUrl] isHTML:NO];
UIImage *imageB = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: myUrl]]];
NSData *exportData = UIImageJPEGRepresentation(imageB ,1.0);
[mailComposer addAttachmentData:exportData mimeType:#"image/jpeg" fileName:img_Title];
[self presentModalViewController:mailComposer animated:YES];
}
//release the mailComposer as it is now being managed as the UIViewControllers modalViewController.
[mailComposer release];
} else if (buttonIndex == 2) {
//cancel
}
[self startChromeDisplayTimer];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
if (result == MFMailComposeResultFailed) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void) exportPhoto
{
if ([dataSource_ respondsToSelector:#selector(exportImageAtIndex:)])
[dataSource_ exportImageAtIndex:currentIndex_];
[self startChromeDisplayTimer];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Actions"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Save to gallery", #"Email",nil];
[actionSheet showInView:[self view]];
[actionSheet release];
}
#end

First of all, tel me if you want to show Status Bar or not.
If you dont want to show then, in nib of controller's, you can select its view, and under its properties set StatusBar to NONE, thar time it wont show status bar... and you can set vew size to (320*480) or else with status bar it will be (320*460) and 20 pixels will be reserved for status bar.
Other ways to do (without using above method)
Can hide StatusBar from info.plist also, by setting property UIStatusBarHidden property to YES. (To hide the status bar when the app launches)
Programmatically can be done, add line to appDelegate's applicationDidFinishLaunching method,
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];

Related

How to hide/Remove the search bar on Contact Picker

I am adding a contact picker in my app, however, I do not want the search functionality.
How to hide/Remove the search bar on Contact Picker (ABPeoplePickerNavigationController)?
static BOOL foundSearchBar = NO;
- (void)findSearchBar:(UIView*)parent mark:(NSString*)mark {
for( UIView* v in [parent subviews] ) {
//if( foundSearchBar ) return;
NSLog(#"%#%#",mark,NSStringFromClass([v class]));
if( [v isKindOfClass:[UISearchBar class]] ) {
[(UISearchBar*)v setTintColor:[UIColor blackColor]];
v.hidden=YES;
// foundSearchBar = YES;
break;
}
if( [v isKindOfClass:[UITableView class]] ) {
CGRect temp =v.frame;
temp.origin.y=temp.origin.y-44;
temp.size.height=temp.size.height+44;
v.frame=temp;
//foundSearchBar = YES;
break;
}
[self findSearchBar:v mark:[mark stringByAppendingString:#"> "]];
}
}
call above method after picker is presented as below:
-(void)showPeoplePickerController
{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// Display only a person's phone, email, and birthdate
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty],[NSNumber numberWithInt:kABPersonAddressProperty],nil];
picker.displayedProperties = displayedItems;
// Show the picker
[self presentViewController:picker animated:YES completion:nil];
[self findSearchBar:[picker view] mark:#"> "];
[picker release];
}
-(void)showAddressBook {
ABPeoplePickerNavigationController *addressBook = [[ABPeoplePickerNavigationController alloc] init];
[addressBook setPeoplePickerDelegate:self];
addressBook.delegate = self;
addressBook.navigationBar.topItem.title = #"iPhone Contacts";
UIView *view = addressBook.topViewController.view;
for (UIView *v in view.subviews) {
if ( [v isKindOfClass:[UITableView class]] ) {
CGRect temp = v.frame;
temp.origin.y = temp.origin.y - 44;
temp.size.height = temp.size.height + 44;
v.frame = temp;
}
}
[addressBook release];
}
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([navigationController isKindOfClass:[ABPeoplePickerNavigationController class]]) {
UISearchDisplayController *searchDisplayController = navigationController.topViewController.searchDisplayController;
[searchDisplayController.searchBar setHidden:YES];
}
}

Implement Simple Payment Using PayPal in iPhone Application?

I am trying to implement Making a Simple Payment through PayPal in IOS Application. I tried the PayPal API. The Sample code given By API is working fine. I am trying to implement in my application it wont getting correctly it getting crash . Is there any simplest way to achieve this.
I added Libray Files. and follow the code :
[PayPal getPayPalInst].feePayer = feePayer;
[PayPal getPayPalInst].shippingEnabled = shippingSwitch.on;
[PayPal getPayPalInst].dynamicAmountUpdateEnabled = dynamicAmountCalculationSwitch.on;
if (isAdvanced) {
[[PayPal getPayPalInst] advancedCheckoutWithPayment:advancedPayment];
} else {
[[PayPal getPayPalInst] checkoutWithPayment:payment];
}
For payment action it will getting erro
Please Help in this issue.
Thanks In Advance.
After importing entire paypal library use this code
#import "PayPal.h"
typedef enum PaymentStatuses {
PAYMENTSTATUS_SUCCESS,
PAYMENTSTATUS_FAILED,
PAYMENTSTATUS_CANCELED,
} PaymentStatus;
#interface Paybypal : UIViewController<PayPalPaymentDelegate>{
#private
CGFloat y;
BOOL resetScrollView;
PaymentStatus status;
}
#property(nonatomic,retain)NSString *details;
#property(nonatomic,retain)NSDecimalNumber *totalvaluemin;
in .m file
#import "Paybypal.h"
#import "PayPalPayment.h"
#import "PayPalAdvancedPayment.h"
#import "PayPalAmounts.h"
#import "PayPalReceiverAmounts.h"
#import "PayPalAddress.h"
#import "PayPalInvoiceItem.h"
#import "SuccessViewController.h"
#define SPACING 3.
#interface Paybypal ()
#end
#implementation Paybypal
#synthesize details,totalvaluemin;
-(void)addLabelWithText:(NSString *)text andButtonWithType:(PayPalButtonType)type withAction:(SEL)action {
UIFont *font = [UIFont boldSystemFontOfSize:14.];
CGSize size = [text sizeWithFont:font];
UIButton *button = [[PayPal getPayPalInst] getPayButtonWithTarget:self andAction:action andButtonType:type];
CGRect frame = button.frame;
frame.origin.x = round((self.view.frame.size.width - button.frame.size.width) / 2.);
frame.origin.y = round(y + size.height);
button.frame = frame;
[self.view addSubview:button];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x, y, size.width, size.height)] autorelease];
label.font = font;
label.text = text;
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
y += size.height + frame.size.height + SPACING;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
self.view.autoresizesSubviews = FALSE;
UIColor *color = [UIColor groupTableViewBackgroundColor];
if (CGColorGetPattern(color.CGColor) == NULL) {
color = [UIColor lightGrayColor];
}
self.title = #"";
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Background.JPG"]];
backgroundView.frame = self.view.bounds;
[[self view] addSubview:backgroundView];
status = PAYMENTSTATUS_CANCELED;
y = 2.;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake((self.view.frame.size.width - 125), 222, 75, 25);
[button setTitle:#"" forState:UIControlStateNormal];
[button addTarget:self action:#selector(RetryInitialization) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self addLabelWithText:#"" andButtonWithType:BUTTON_294x43 withAction:#selector(simplePayment)];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)simplePayment {
[PayPal getPayPalInst].shippingEnabled = TRUE;
[PayPal getPayPalInst].dynamicAmountUpdateEnabled = TRUE;
[PayPal getPayPalInst].feePayer = FEEPAYER_EACHRECEIVER;
PayPalPayment *payment = [[[PayPalPayment alloc] init] autorelease];
payment.recipient = #"receiver email id";
payment.paymentCurrency = #"USD";
payment.description = [NSString stringWithFormat:#"%#",details];
payment.merchantName = #"receiver name";
payment.subTotal = totalvaluemin;
payment.invoiceData = [[[PayPalInvoiceData alloc] init] autorelease];
payment.invoiceData.totalShipping = [NSDecimalNumber decimalNumberWithString:#"0"];
payment.invoiceData.totalTax = [NSDecimalNumber decimalNumberWithString:#"0.00"];
payment.invoiceData.invoiceItems = [NSMutableArray array];
PayPalInvoiceItem *item = [[[PayPalInvoiceItem alloc] init] autorelease];
item.totalPrice = payment.subTotal;
item.name =[NSString stringWithFormat:#"%#",details];
[payment.invoiceData.invoiceItems addObject:item];
[[PayPal getPayPalInst] checkoutWithPayment:payment];
}
#pragma mark -
#pragma mark PayPalPaymentDelegate methods
-(void)RetryInitialization
{
//[PayPal initializeWithAppID:#"APP-80W284485P519543T" forEnvironment:ENV_SANDBOX];
//DEVPACKAGE
//[PayPal initializeWithAppID:#"APPLIVEID" forEnvironment:ENV_LIVE];
// [PayPal initializeWithAppID:#"anything" forEnvironment:ENV_NONE];
}
- (void)paymentSuccessWithKey:(NSString *)payKey andStatus:(PayPalPaymentStatus)paymentStatus {
NSString *severity = [[PayPal getPayPalInst].responseMessage objectForKey:#"severity"];
NSLog(#"severity: %#", severity);
NSString *category = [[PayPal getPayPalInst].responseMessage objectForKey:#"category"];
NSLog(#"category: %#", category);
NSString *errorId = [[PayPal getPayPalInst].responseMessage objectForKey:#"errorId"];
NSLog(#"errorId: %#", errorId);
NSString *message = [[PayPal getPayPalInst].responseMessage objectForKey:#"message"];
NSLog(#"message: %#", message);
status = PAYMENTSTATUS_SUCCESS;
}
- (void)paymentFailedWithCorrelationID:(NSString *)correlationID {
NSString *severity = [[PayPal getPayPalInst].responseMessage objectForKey:#"severity"];
NSLog(#"severity: %#", severity);
NSString *category = [[PayPal getPayPalInst].responseMessage objectForKey:#"category"];
NSLog(#"category: %#", category);
NSString *errorId = [[PayPal getPayPalInst].responseMessage objectForKey:#"errorId"];
NSLog(#"errorId: %#", errorId);
NSString *message = [[PayPal getPayPalInst].responseMessage objectForKey:#"message"];
NSLog(#"message: %#", message);
status = PAYMENTSTATUS_FAILED;
}
- (void)paymentCanceled {
status = PAYMENTSTATUS_CANCELED;
}
- (void)paymentLibraryExit {
UIAlertView *alert = nil;
switch (status) {
case PAYMENTSTATUS_SUCCESS:
[self.navigationController pushViewController:[[[SuccessViewController alloc] init] autorelease] animated:TRUE];
break;
case PAYMENTSTATUS_FAILED:
alert = [[UIAlertView alloc] initWithTitle:#"Order failed"
message:#"Your order failed. Touch \"Pay with PayPal\" to try again."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
break;
case PAYMENTSTATUS_CANCELED:
alert = [[UIAlertView alloc] initWithTitle:#"Order canceled"
message:#"You canceled your order. Touch \"Pay with PayPal\" to try again."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
break;
}
[alert show];
[alert release];
}
- (PayPalAmounts *)adjustAmountsForAddress:(PayPalAddress const *)inAddress andCurrency:(NSString const *)inCurrency andAmount:(NSDecimalNumber const *)inAmount
andTax:(NSDecimalNumber const *)inTax andShipping:(NSDecimalNumber const *)inShipping andErrorCode:(PayPalAmountErrorCode *)outErrorCode {
//do any logic here that would adjust the amount based on the shipping address
PayPalAmounts *newAmounts = [[[PayPalAmounts alloc] init] autorelease];
newAmounts.currency = #"SGD";
newAmounts.payment_amount = (NSDecimalNumber *)inAmount;
//change tax based on the address
if ([inAddress.state isEqualToString:#"CA"]) {
newAmounts.tax = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:#"%.2f",[inAmount floatValue] * .1]];
} else {
newAmounts.tax = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:#"%.2f",[inAmount floatValue] * .08]];
}
newAmounts.shipping = (NSDecimalNumber *)inShipping;
//if you need to notify the library of an error condition, do one of the following
//*outErrorCode = AMOUNT_ERROR_SERVER;
//*outErrorCode = AMOUNT_CANCEL_TXN;
//*outErrorCode = AMOUNT_ERROR_OTHER;
return newAmounts;
}
- (NSMutableArray *)adjustAmountsAdvancedForAddress:(PayPalAddress const *)inAddress andCurrency:(NSString const *)inCurrency
andReceiverAmounts:(NSMutableArray *)receiverAmounts andErrorCode:(PayPalAmountErrorCode *)outErrorCode {
NSMutableArray *returnArray = [NSMutableArray arrayWithCapacity:[receiverAmounts count]];
for (PayPalReceiverAmounts *amounts in receiverAmounts) {
//leave the shipping the same, change the tax based on the state
if ([inAddress.state isEqualToString:#"CA"]) {
amounts.amounts.tax = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:#"%.2f",[amounts.amounts.payment_amount floatValue] * .1]];
} else {
amounts.amounts.tax = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:#"%.2f",[amounts.amounts.payment_amount floatValue] * .08]];
}
[returnArray addObject:amounts];
}
//if you need to notify the library of an error condition, do one of the following
//*outErrorCode = AMOUNT_ERROR_SERVER;
//*outErrorCode = AMOUNT_CANCEL_TXN;
//*outErrorCode = AMOUNT_ERROR_OTHER;
return returnArray;
}
#pragma mark -
#pragma mark UITextFieldDelegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return TRUE;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
resetScrollView = FALSE;
return TRUE;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
resetScrollView = TRUE;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
self.view.frame = CGRectMake(0., -216., self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (resetScrollView) {
resetScrollView = FALSE;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
self.view.frame = CGRectMake(0., 0., self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return TRUE;
}
#end

UIScrollView content not updating

I have UIScrollView in stage and paging functionality enabled to move back/next.
I have added 10 subviews in UIScrollView. When I have modify the content inside the subviews, then it is not reflected in UIScrollView.
Book.m
- (void) initializeWithXML:(NSString *)XMLURLString {
NSData *xmlData;
NSString *url;
if ( ![applicationData getMode] ) {
resourceRootURL = [[applicationData getAssetsPath] stringByAppendingPathComponent:#"phone/"];
} else {
resourceRootURL = [applicationData getAssetsPath];
}
url = [resourceRootURL stringByAppendingPathComponent:XMLURLString];
//NSLog(#"Root URL...%#", url);
if ([url rangeOfString:#"http"].location != NSNotFound) {
xmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
} else {
xmlData = [NSData dataWithContentsOfFile:url];
}
bookContentArray = [self grabXML:xmlData andQuery:#"//page"];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
for(int i = 0; i < [bookContentArray count]; i++) {
[viewControllers addObject:[NSNull null]];
}
if ( isTwoPage ) {
kNumberOfPages = [bookContentArray count];
} else {
kNumberOfPages = [bookContentArray count]/2;
}
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
scrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor blackColor];
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = [self contentSizeForPagingScrollView];
scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
scrollView.delegate = self;
self.view = scrollView;
kNumberOfPages = [bookContentArray count];
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadPage:0];
}
- (void) loadPage:(int)number {
// Calculate which pages are visible
int firstNeededPageIndex = MAX(number-1, 0);
int lastNeededPageIndex = MIN(number+1, [viewControllers count] - 1);
//NSLog(#"%d,%d",firstNeededPageIndex,lastNeededPageIndex);
// Recycle no-longer-visible pages
for(int i = 0; i < [viewControllers count]; i++) {
ImageScrollView *page = [viewControllers objectAtIndex:i];
if ((NSNull *)page != [NSNull null]) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
//NSLog(#"removed page %d", page.index);
[page removeImages];
[page removeFromSuperview];
page = nil;
[viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
}
}
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
if ( number == 0 ) {
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
} else if ( number == [bookContentArray count]-1) {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
} else {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
}
}
- (void)loadScrollViewWithPage:(int)page {
ImageScrollView *pageController = [[ImageScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
pageController.index = page;
pageController.myDelegate = self;
pageNumber = pageControl.currentPage;
pageController.isTwoPage = isTwoPage;
if ( pageNumber == page ) {
pageController.isCurrentPage = YES;
} else {
pageController.isCurrentPage = NO;
}
[controller setImageURL:leftURL andRightURL:rightURL andPriority:0];
[scrollView addSubview:controller];
}
ImageScrollView.m
- (void) setImageURL:(NSString *)leftURL andRightURL:(NSString *)rightURL andPriority:(int)priority {
[imageView removeFromSuperview];
[imageView release];
imageView = nil;
if ( !isTwoPage ) {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
} else {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
}
if ( isTwoPage ) {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = CGRectMake(0, 43, 512, 682);
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = CGRectMake(512, 43, 512, 682);
} else {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = imageView.frame;
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = imageView.frame;
}
leftImage.delegate = self;
rightImage.delegate = self;
if ( isFirstPage ) {
[leftImage.view setHidden:YES];
[rightImage.view setHidden:NO];
} else if ( isLastPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else if ( !isTwoPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:NO];
}
imageView.backgroundColor = [UIColor whiteColor];
[self addSubview:imageView];
[leftImage setImageURL:leftURL];
[rightImage setImageURL:rightURL];
[imageView addSubview:leftImage.view];
[imageView addSubview:rightImage.view];
}
FBPage.m
- (void) setImageURL:(NSString *)url {
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 768, 1024);
imageView.center = self.view.center;
imageView.userInteractionEnabled = TRUE;
[container addSubview:imageView];
[activityIndicator setHidden:YES];
if ([url rangeOfString:#"http"].location != NSNotFound) {
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:[NSURL URLWithString:url]];
if (cachedImage) {
[imageView setImage:cachedImage];
isImageLoaded = YES;
[self addScrollView];
} else {
[activityIndicator setHidden:NO];
[activityIndicator startAnimating];
[manager downloadWithURL:[NSURL URLWithString:url] delegate:self options:0 success:^(UIImage *image){
[activityIndicator stopAnimating];
[activityIndicator setHidden:YES];
[imageView setImage:image];
isImageLoaded = YES;
[self addScrollView];
} failure:nil];
}
imageURL = url;
} else {
imageView.image = [UIImage imageWithContentsOfFile:url];
isImageLoaded = YES;
[self addScrollView];
}
}
- (void) setHotspotURL:(NSString *)URL {
[hotspot.view removeFromSuperview];
[hotspot release];
hotspot = nil;
hotspot = [[FBHotspot alloc] initWithNibName:#"FBHotspot" bundle:nil];
hotspot.view.frame = CGRectMake(0, 0, 768, 1024);
hotspot.delegate = self;
[hotspot setURL:URL];
[container addSubview:hotspot.view];
}
**When I am calling setHotspotURL and it’s not updating to the scrollview.**
Make sure you inserted your content at the correct position within the scroll view. Set the contentSize accordingly. Try to call layoutSubviews of the UIScrollView instance

How to use UISwipeGestureRecognizer on UIButton?

I have a UIbutton which i want to work as a joystick. So i am trying to add some gesture recognizer on the same button.
I have this in my code right now:
#implementation CUETutorialSixteenClusterRootController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:#"CUETutorialLandscapeClusterRootController" bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (id)init {
self = [super init];
if (self) {
_leftInActiveView = [[NSMutableArray alloc] init];
_rightInActiveViews = [[NSMutableArray alloc] init];
_centerInActiveViews = [[NSMutableArray alloc]init];
_centerActiveViews = [[NSMutableArray alloc]init];
UIImageView * inActLeftView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease];
inActLeftView.image = [UIImage imageNamed:#"1-am-station_01.png"];
[_leftInActiveView addObject:inActLeftView];
UIImageView * inActRightView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease];
inActRightView.image = [UIImage imageNamed:#"1-am-station_03.png"];
[_rightInActiveViews addObject:inActRightView];
NSArray * imagesName = [NSArray arrayWithObjects:[UIImage imageNamed:#"1_start.png"],
[UIImage imageNamed:#"2_apptray_audio_selected.png"],
[UIImage imageNamed:#"2_apptray_phone_selected.png"],
[UIImage imageNamed:#"4_phonemenu_contacts_highlighted.png"],
[UIImage imageNamed:#"5_phonemenu_recentcalls_highlighted.png"],
[UIImage imageNamed:#"6_recent_calls.png"],
[UIImage imageNamed:#"7_bottom_hit.png"],
[UIImage imageNamed:#"8_recent_call_details.png"],
[UIImage imageNamed:#"9_calling.png"],
[UIImage imageNamed:#"9_calling_myphone.png"],
[UIImage imageNamed:#"9_call_ended.png"],nil];
NSLog(#"Count:%d",[imagesName count]);
for (int i =0; i<11; i++) {
UIImageView * inActCenterView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 226, 98)] autorelease];
inActCenterView.image = [imagesName objectAtIndex:i];
[_centerActiveViews addObject:inActCenterView];
[_centerInActiveViews addObject:inActCenterView];
}
}
return self;
}
-(void)showClusterType:(clusterType)cluster{
switch (cluster) {
//Base Cluster is analog
case kClusterTypeBase:
{
[self.fullClusterContainerView removeFromSuperview];
[self.view addSubview:self.baseClusterContainerView];
[self.view addSubview:self.baseClusterImageView];
CUETutorialSixteenAplicationHomeScreen * clusAppScreen = [[CUETutorialSixteenAplicationHomeScreen alloc] initWithDelegate:self];
clusAppScreen.dataSource = self;
clusAppScreen.rootController = self;
self.clusterHomeScreen = clusAppScreen;
[self.baseClusterContainerView addSubview:clusAppScreen.view];
[clusAppScreen release];
/*
[self.view bringSubviewToFront:self.steeringWheelImageView];
[self.view bringSubviewToFront:self.promptLabel];
[self.view bringSubviewToFront:[self.view viewWithTag:kFavDownButton]];
[self.view bringSubviewToFront:[self.view viewWithTag:kFavUpButton]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonUp]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonLeft]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonRight]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonDown]];
[self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonCenter]];
*/
[self.view bringSubviewToFront:self.promptLabel];
}
break;
//Full cluster is digital
case kClusterTypeFull:
{
[self.baseClusterImageView removeFromSuperview];
[self.baseClusterContainerView removeFromSuperview];
[self.view addSubview:self.fullClusterContainerView];
}
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
_step =1;
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.gestureRecieverButton addGestureRecognizer:recognizer];
[recognizer release];
recognizer = nil;
}
- (void)viewDidUnload
{
[super viewDidUnload];
time=0;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void)dealloc{
[super dealloc];
[_leftInActiveView release];
[_rightInActiveViews release];
[_centerActiveViews release];
[_centerInActiveViews release];
}
-(void)animateToView:(UIView *)aview{
if(aview){
//CGRect rect = [aview convertRect:aview.bounds toView:self.view];
CGRect rect = aview.frame;
[self animateHandFromRect:self.promptLabel.frame toRect:rect];
}
else{
[self animateHandToNSValueRect:nil];
}
}
-(void)finishTutorial{
[self displayPromptText:#"TUTORIAL COMPLETE"];
[self displayNavigationBarWithTitle:#"TUTORIAL COMPLETE"];
[self performBlock:^{
[self.clusterHomeScreen.view removeFromSuperview];
[self showScoresScreen];
}afterDelay:1];
}
-(void)animateHandToCenterButton
{
[self displayPromptText:#"TAP RIGHT SIDE CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE CENTER BUTTON"];
[self animateToView:self.rCenterButton];
}
-(void)animateHandToDownButton {
[self displayPromptText:#"TAP RIGHT SIDE DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE DOWN BUTTON"];
[self animateToView:self.rDownButton];
}
-(void)animateHandToUpButton{
[self displayPromptText:#"TAP RIGHT SIDE UP BUTTON"];
[self displayNavigationBarWithTitle:#"TAP RIGHT SIDE UP BUTTON"];
[self animateToView:self.rUpButton];
}
-(void)replaceCallScreen{
[self performBlock:^{
[self pressedRightButtonCenter];
} afterDelay:2.0];
}
-(void)changeTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(startTicking) userInfo:nil repeats:YES];
}
-(void)startTicking {
time = time +1;
self.clusterHomeScreen.callTimer.text = [NSString stringWithFormat:#"00:0%d",time];
if (time==9) {
[timer invalidate];
}
}
-(void) animateHandToVolUpButton {
[self displayPromptText:#"TAP VOLUME UP BUTTON"];
[self displayNavigationBarWithTitle:#"TAP VOLUME UP BUTTON"];
[self animateToView:self.rVolUpButton];
}
-(void) animateHandToVolDownButton {
[self displayPromptText:#"TAP VOLUME DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP VOLUME DOWN BUTTON"];
[self animateToView:self.rVolDownButton];
}
-(void)removeVolumeView {
[self pressedRightButtonCenter];
[self.clusterHomeScreen runNextStep];
}
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.%#",recognizer);
}
-(IBAction)buttonClicked:(id)sender
{
if ([sender tag]==kRightButtonDown && _step ==1)
{
[self pressedRightButtonDown];
[self displayPromptText:#"TAP AGAIN"];
[self displayNavigationBarWithTitle:#"TAP AGAIN"];
_step++;
}
else if ([sender tag]==kRightButtonDown && _step == 2)
{
[self pressedRightButtonDown];
[self animateToView:self.rCenterButton];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step == 3)
{
[self pressedRightButtonCenter];
[self animateToView:self.rUpButton];
_step++;
}
else if ([sender tag]==kRightButtonUp && _step == 4)
{
[self pressedRightButtonUp];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
[self animateToView:self.rCenterButton];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==5){
[self pressedRightButtonCenter];
[self displayPromptText:#"TAP DOWN BUTTON"];
[self displayNavigationBarWithTitle:#"TAP DOWN BUTTON"];
[self animateToView:self.rDownButton];
_step++;
}
else if([sender tag]==kRightButtonDown && _step==6){
[self pressedRightButtonDown];
[self animateToView:self.rCenterButton];
[self displayPromptText:#"TAP CENTER BUTTON"];
[self displayNavigationBarWithTitle:#"TAP CENTER BUTTON"];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==7){
[self pressedRightButtonCenter];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==8)
{
[self pressedRightButtonCenter];
self.clusterHomeScreen.callTimer.hidden = NO;
[self.clusterHomeScreen runNextStep];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==9)
{
self.clusterHomeScreen.callTimer.hidden = YES;
[self pressedRightButtonCenter];
_step++;
}
else if ([sender tag]==kRightButtonCenter && _step==10)
{
[self pressedRightButtonCenter];
[self finishTutorial];
}
else {
[self displayVisualFeedback:kVisualFeedBackIncorrectAnswer];
}
}
#pragma CUEClusterHomeSreenDataSource Methods
-(NSInteger)numberOfViewsForLeftScreen {
return [_leftActiveViews count];
}
-(NSInteger)numberOfViewsForRightScreen{
return [_rightActiveViews count];
}
-(NSInteger)numberOfViewsForCenterScreen {
return [_centerActiveViews count];
}
-(UIImageView *)inActiveViewForLeftScreenAtIndex:(NSInteger)index{
//if((index < [_leftInActiveView count]) && (index >=0)){
return [_leftInActiveView objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForLeftScreenAtIndex:(NSInteger)index{
// if((index < [_leftActiveViews count]) && (index >=0)){
return [_leftActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)inActiveViewForRightScreenAtIndex:(NSInteger)index{
//if((index < [_rightInActiveViews count]) && (index >=0)){
return [_rightInActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForRightScreenAtIndex:(NSInteger)index{
//if((index < [_rightActiveViews count]) && (index >=0)){
return [_rightActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)inActiveViewForCenterScreenAtIndex:(NSInteger)index{
//if((index < [_leftInActiveView count]) && (index >=0)){
return [_centerInActiveViews objectAtIndex:index];
//}
//return nil;
}
-(UIImageView *)activeViewForCenterScreenAtIndex:(NSInteger)index{
// if((index < [_leftActiveViews count]) && (index >=0)){
return [_centerActiveViews objectAtIndex:index];
//}
//return nil;
}
This is the error i am getting now:
-[CUETutorialSixteenClusterRootController handleSwipeFrom:]: unrecognized selector sent to instance 0x79b71b0
2012-03-28 13:25:55.724 CUETrainer[1788:11f03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CUETutorialSixteenClusterRootController handleSwipeFrom:]: unrecognized selector sent to instance 0x79b71b0'
But its not actually doing anything.
Please help!!!!
The error says unrecognized selector sent to instance 0x79b71b0' most probably the names in .h and .m are different..
The target of the gesture recognizer needs to be the object that implements the selector. What's happening now when a swipe happens is that the gesture recognizer is doing [self.gestureReceiverButton handleSwipeFrom:recognizer]
You probably want this:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
Change every instance of:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
to
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
Problem solved. Be very careful with your #selectors, what you place in this macro is NOT validated by the compiler. Code complete will help you with this.
Also you are adding four gesture recognizers to one view, while you can accomplish the same task with one. You can combine the valid directions using the bitwise OR operator, like so:
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown)];

change orientation dynamically

I want to change my view layout dynamically. how i define in my code? In my code i define as
const CGFloat kScrollObjHeight = 460.0;
const CGFloat kScrollObjWidth = 320.0;
const NSUInteger kNumImages = 32;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
//imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image0.jpg"]];
[scrollView1 addSubview:imageView];
[scrollView1 setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
scrollView1.minimumZoomScale = 1;
scrollView1.maximumZoomScale = 3;
scrollView1.delegate = self;
[scrollView1 setScrollEnabled:YES];
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"page-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *ImageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = ImageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
ImageView.frame = rect;
ImageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:ImageView];
[ImageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
switch(toInterfaceOrientation){
case (UIInterfaceOrientationLandscapeLeft):
{
landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:#"LandscapeModeViewController" bundle:nil];
[self.view addSubview:landscapeModeViewController.view];
/*UIAlertView *info = [[UIAlertView alloc] initWithTitle:#"Info" message:#"change Mode" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[info show];
[info release];*/
}
break;
case(UIInterfaceOrientationLandscapeRight):
{
//landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:#"LandscapeModeViewController" bundle:nil];
//[self.view addSubview:landscapeModeViewController.view];
UIAlertView *info = [[UIAlertView alloc] initWithTitle:#"Info" message:#"change Mode" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[info show];
[info release];
}
break;
case(UIInterfaceOrientationPortrait):
{
//portraitMode=[[PortraitMode alloc] initWithNibName:#"PortraitMode" bundle:nil];
//[self.view addSubview:portraitMode.view];
UIAlertView *info = [[UIAlertView alloc] initWithTitle:#"Info" message:#"change Mode" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[info show];
[info release];
}
break;
/*if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
UIAlertView *info = [[UIAlertView alloc] initWithTitle:#"Info" message:#"Select Landscape mode." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[info show];
[info release];
[scrollView1 release];
landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:#"LandscapeModeViewController" bundle:nil];
[self.view addSubview:landscapeModeViewController.view];
}
else{
UIAlertView *info = [[UIAlertView alloc] initWithTitle:#"Info" message:#"Select Portrait Mode" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[info show];
[info release];
portraitMode=[[PortraitMode alloc] initWithNibName:#"PortraitMode" bundle:nil];
[self.scrollView1 addSubview:portraitMode.view];
} */
}
}
- (void)dealloc
{
[scrollView1 release];
[imageView release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
in above code when rotate device then uialerview execute but previous view not release and landsacpe mode shown behind him. so what i will do for that?
You can set autoresize mask to each view in your layout in whatever manner you want.
For example, if you want button to show always on top left corner, you can set auto resize mask to flexible top and left..
If your layout is totally different in portrait and landscape, then you need to set frames in willRotateToInterfaceOrientation.
For example if you want your button on top left in portrait and in center in landscape, than you need to set frame in the above delegate method.
Edit Answer:
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
if(!landscapeModeViewController){
landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:#"LandscapeModeViewController" bundle:nil];
}
[self.view addSubview:landscapeModeViewController.view];
[self.view bringSubviewToFront:landscapeModeViewController.view];
}
else if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
if(!portraitMode){
portraitMode=[[PortraitMode alloc] initWithNibName:#"PortraitMode" bundle:nil];
}
[self.view addSubview:portraitMode.view];
[self.view bringSubviewToFront:portraitMode.view];
}