iAD disapearing when seguing to modal view - segue

I have a unique viewController for landscape mode, the problem is that when I segue to the landscape view controller (modal segue) the iAD disapears and it seems to stop receiving delegate methods as well. Only when I tap the tab bar to go to another view and then back again does it show again
- (void)orientationChanged:(NSNotification *)notification
{
NSLog(#"orientation changed");
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView && isThisView)
{
//[self.view bringSubviewToFront:self.backgroundImageView];
[self performSegueWithIdentifier:#"DisplayLandscapeView" sender:self];
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
self.bannerView.hidden = NO;
//[self.view sendSubviewToBack:self.backgroundImageView];
}
}

Related

How to hide navigationBar when scrolling collectionView in ios 11?

In iOS 11 searchBar hides when scrolling tableView if we set to hide. how can I hide searchBar, navigationBar and tabBar when scrolling collectionView up ? And unhide them all when scrolling down ? Thanks for your help...
Subclass UIScrollViewDelegate in your UIViewController (i.e. class ViewController: UIViewController, UIScrollViewDelegate { codes... })
Implement scrollViewDidScroll Delegate method
func scrollViewDidScroll(scrollView: UIScrollView) {
let pan = scrollView.panGestureRecognizer
let velocity = pan.velocityInView(scrollView).y
if velocity < -5 {
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.setToolbarHidden(true, animated: true)
} else if velocity > 5 {
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.setToolbarHidden(false, animated: true)
}
}
Translating KTang's accepted answer to Objective-C:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
CGFloat velocity = [pan velocityInView:scrollView].y;
if (velocity < -5) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
} else if (velocity > 5) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
}
}

When searching how to make UITableViewCellAccessoryCheckmark the correct cell?

I have a view controller with 2 arrays, one for normal results; the other for filtered/search results based off text entry.
Only one cell can have the UITableViewCellAccessoryCheckmark at a time.
My problem can be described thusly;
IE:
View controller is fed a Venue object; and is marked with
UITableViewCellAccessoryCheckmark. This is expected, and correct.
User types in a search query, the search results array is used;
however the UITableViewCellAccessoryCheckmark is no longer on the
Venue that was previously checked in step 1.
I am not sure why its not checking the cell.
Visual examples;
Original view. Cobo Arena is the pre-selected venue
-
When typing; The checkmark is not in the right place
-
More typing: The checkmark is now gone
-
The code is below
- (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
FCVenue *venue;
if (self.isSearching)
{
venue = [self.searchResults objectAtIndex:indexPath.row];
}
else
{
venue = [self.venues objectAtIndex:indexPath.row];
}
if ([indexPath isEqual:self.selectedIndexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", venue.name];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", venue.location];
}
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView cellForRowAtIndexPath:self.selectedIndexPath].accessoryType = UITableViewCellAccessoryNone;
self.selectedIndexPath = indexPath;
[self.tableView reloadRowsAtIndexPaths:#[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
FCVenue *venue;
if (self.searching)
{
venue = [self.searchResults objectAtIndex:indexPath.row];
}
else
{
venue = [self.venues objectAtIndex:indexPath.row];
}
self.selectedVenue = venue;
// This method fires a completion block and dismisses the view controller
if (self.completionBlock)
{
self.completionBlock( self, self.selectedVenue );
}
[self.navigationController dismissViewControllerAnimated:YES completion:^{
}];
}
That's happening because you are storing the index of the full table to display the checkmark. Instead you should compare the FCVenue object to see if that is the one checked or not.
So the code should be something like this, it is not tested though:
- (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
FCVenue *venue;
if (self.isSearching)
{
venue = [self.searchResults objectAtIndex:indexPath.row];
}
else
{
venue = [self.venues objectAtIndex:indexPath.row];
}
if ([venue isEqual:self.selectedVenue]) // You may want to compare just the id or any other unique property
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// if you opt for keeping the selectedIndexPath property you need to refresh it here.
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", venue.name];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", venue.location];
}
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//[self.tableView cellForRowAtIndexPath:self.selectedIndexPath].accessoryType = UITableViewCellAccessoryNone;
// Here you may want to do a loop over all the possible index path to clean the state or keep storing the selected indexPath just to clear the mark when the selected venue changes.
FCVenue *venue;
if (self.searching)
{
venue = [self.searchResults objectAtIndex:indexPath.row];
}
else
{
venue = [self.venues objectAtIndex:indexPath.row];
}
self.selectedVenue = venue;
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// This method fires a completion block and dismisses the view controller
if (self.completionBlock)
{
self.completionBlock( self, self.selectedVenue );
}
[self.navigationController dismissViewControllerAnimated:YES completion:^{
}];
}
In any case the general idea is that you need to tie the venue with the check and not with at indexPath as the indexPath will change with the search.

Checkmark won't show in TableViewCell on iOS7

I'm working on a weird issue right now. My Apps Deployment Target is set to iOS6, so I want to support both iOS6 and iOS7.
I just have a simple UITableView, in which the user can select the preferred notification sound.
The code for - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CheckmarkCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell setTintColor:[UIColor redColor]];
if (indexPath.section == 0){
cell.textLabel.text = [_availableSounds objectAtIndex:indexPath.row];
if (indexPath.row == _checkedSoundIndexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
else {
// Unrelated, another settings cell
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
My - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath looks like the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section != 0) {
return;
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[[self.tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
if (_checkedSoundIndexPath != indexPath) {
[[self.tableView cellForRowAtIndexPath:_checkedSoundIndexPath] setAccessoryType:UITableViewCellAccessoryNone];
}
_checkedSoundIndexPath = indexPath;
}
The problem is that an iOS7 iPhone won't show the checkmark as expected. Running the same code on an iOS6 iPhone works as expected. I tried to insert [cell setTintColor:[UIColor redColor]]; but without any luck. Even if I remove all AccessoryType related code and add the checkmark in my storyboard nothing appears. See screenshots below (first is iOS6 and second is iOS5).
Does anyone have an idea ? Or is it a bug in iOS7 ?
Thanks in advance !
Edit:
Even if I make a new simple UITableViewController, with just 5 cells with the Accessory set to UITableViewAccessoryTypeCheckmark, the Checkmarks won't appear on iOS7.
I had a similar problem and I solved this issues changing the tint color of the uitableview
I changed the tintcolot of uitable by InterfaceBuilder to Default color
or
tableView.tintColor = [UIColor blackColor];
I had the exact same problem as you are for a long time.
In my case I implemented some appearance tweaks. And one of them was
[[UIView appearance] setTintColor:[UIColor whiteColor]];
Try to find in your project global things like that.
In my application it working perfect,check it
//take it in .h file mutable arSelectedRows;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
//Do anything you want for cell here
if([arSelectedRows containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[arSelectedRows addObject:indexPath];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[arSelectedRows removeObject:indexPath];
}
NSLog(#"id are here :%#",arSelectedIDs);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
may be ,it will helpful.
No, There is No Problem with UITableViewCellAccessoryCheckmark in iOS 7,Make sure you have implemented correct logic for it.
I think the problem is with your checksoundIndexpath, please check whether it has a correct indexpath, Or First check with hardcoded indexpath.
you are not initializing the checksoundindxpath.
Also I noticed you are not assigning the selected row indexpath to _checkSoundIndexpath, Only you are checking if both indexpaths(current indexpaths and _checksoundindexpath) are equal, But if they are different then you should assign the selected indedxpath to _checksound indexpath.
I can solve this problem by set tint color for Cell
It didnt show because cell tint in white ,and cell selectionstyle is none
cell.tintColor = [UIColor blackColor];
if(cell.selected){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
I would rather reload tableview (preferably only the affected rows - using reloadRowsAtIndexPaths) after settings appropriate data in didSelectRowAtIndexPath: method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Other logic..
[tableView deselectRowAtIndexPath:indexPath animated:NO]; // Would like to look for an alternative method to avoid this refresh??
NSMutableArray *reloadArray = [NSMutableArray arrayWithCapacity:2];
[reloadArray addObject:indexPath];
[reloadArray addObject:_checkedSoundIndexPath];
self.checkedSoundIndexPath = indexPath; // Should set this before reload
[tableView reloadRowsAtIndexPaths:reloadArray withRowAnimation:UITableViewRowAnimationAutomatic];
}
And, most importantly, add these lines in cellForRowAtIndexPath: method -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// All your other code for this section followed by these lines...
if([_checkedSoundIndexPath compare:indexPath] == NSOrderedSame) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Other sections code
return cell;
}
I know the original question uses a simple UITableViewCell but this might work for someone else with a custom table view cell.
I have a custom UITableViewCell subclass. I tried everything but the checkmark won't show. Eventually I realised that I overrode layoutSubviews but forgot to call [super layoutSubviews]. Which meant the checkmark would not show correctly.
- (void)layoutSubviews
{
[super layoutSubviews];
//...custom layout here
}

Basic iphone app, and breaking point error

I'm trying to learn ios coding and trying to make a foodDiary application.. I've used empty project file and then added the storyboard,view controller and table view controller
when I run my program the tableView Menu shows up with the elements in it, then when I press the + button a view controller shows up which has a textfield and a button inside.. when I type in something and press the button my program crushes and I get breakpoint error,
I get error here:
- (IBAction)addFoodButton:(id)sender {
NSString *newFood = [addFoodText text];
[foodTableViewController addFood:newFood];
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
my partial coding are:
DAYfoodTableViewController.h
#import <UIKit/UIKit.h>
#interface DAYfoodTableViewController : UITableViewController
#property (nonatomic, strong) NSMutableArray *foodArray;
-(void) addFood:(NSString *) newFood;
#end
DAYfoodTableViewController.m
#import "DAYfoodTableViewController.h"
#import "DAYaddFoodViewController.h"
#interface DAYfoodTableViewController ()
#end
#implementation DAYfoodTableViewController
#synthesize foodArray;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DAYaddFoodViewController *addFoodViewController = [segue destinationViewController];
[addFoodViewController setFoodTableViewController:self];
}
-(void) addFood:(NSString *)newFood
{
[foodArray addObject:newFood];
[[self tableView] reloadData];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
foodArray=[[NSMutableArray alloc] initWithObjects:#"Pizza",#"Chips",#"Sandwiches",#"Hot Dogs", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [foodArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"BasicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
int rowNumber=[indexPath row];
NSString *food=[foodArray objectAtIndex:rowNumber];
[[cell textLabel] setText:food];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[foodArray removeObjectAtIndex:[indexPath row] ];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade ];
/*
or (by asim)
if (editingStyle == UITableViewCellEditingStyleDelete) {
[foodArray removeObjectAtIndex:[indexPath row] ];
[[self tableView] reloadData];
}
*/
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
DAYaddFoodTableViewController.h
#import <UIKit/UIKit.h>
#import "DAYfoodTableViewController.h"
#interface DAYaddFoodViewController : UIViewController
#property (nonatomic, weak) DAYfoodTableViewController *foodTableViewController;
- (IBAction)addFoodButton:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *addFoodText;
#end
DAYaddFoodTableView.m
#import "DAYaddFoodViewController.h"
#interface DAYaddFoodViewController ()
#end
#implementation DAYaddFoodViewController
#synthesize foodTableViewController;
#synthesize addFoodText;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)addFoodButton:(id)sender {
NSString *newFood = [addFoodText text];
[foodTableViewController addFood:newFood];
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
#end
The weird thing is that when the program crushes and I re-run the program through the simulator it works perfectly..
what could cause this problem? thanks
It appears from the partial code listing that you didn't finish all of the tasks required. You have made your initial controller to be a delegate, but not defined the delegate as a property in your header file or synthesized it in your implementation file. Additionally, you did not add it to the second controller, but are attempting to call the optional method of your first controller, and calling it incorrectly. I think that is why it is crashing.
Seques, which you've used, coupled with notifications, can accomplish many of the things which delegation can.
You need to study up on notification and seques... which allows you to pass data back and forth between controllers. And, in your case you need to.
Watch the appropriate WWDC videos, and work through some tutorials, and these are some of the best... http://www.raywenderlich.com/tutorials

QLPreviewController hide print button in ios6

How can I hide the print button in QLPreviewController
In IOS5, this code works
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.delegate = self;
previewController.currentPreviewItemIndex = _fileidx;
[[self navigationController] pushViewController:previewController animated:YES];
[previewController.navigationItem setRightBarButtonItem:nil];
but in IOS6, it does`t.
i managed to do it by creating a timer to check for the navigation item and remove it
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(hideRightButton:)
userInfo:nil
repeats:YES];
- (void)hideRightButton:(NSTimer *)timer {
[self inspectSubviewsForView:self.view];
}
- (void)inspectSubviewsForView:(UIView *)view
{
for (UIView *subview in view.subviews)
{
NSLog(#"class detected %#",[subview description]);
if ([subview isKindOfClass:[UINavigationBar class]])
{
UINavigationBar *bar = (UINavigationBar *)subview;
if ([[bar items] count] > 0)
{
UINavigationItem *navItem = [[bar items] objectAtIndex:0];
[navItem setRightBarButtonItem:nil];
{
}
if ([subview isKindOfClass:[UIView class]] && [[subview subviews] count] > 0)
{
[self inspectSubviewsForView:subview];
}
}
}
[self inspectSubviewsForView:subview];
}
}
The issue is that the button seems to be regenerated once the document is ready. I cannot really define "ready" but I wrote a test app and noticed a couple of things:
Setting the NavigationItem's right bar buttons doesn't work in iOS6
Scanning the view hierachy and search for an instance of UIToolbar and set the toolbar's buttons works.
The above hack only works if the document being shown is "ready" fast enough. Big documents take longer.
I came up with a two step solution:
Search the navigation item
Constantly hide it using NSTimer.
Here's some code:
This is from ViewDidAppear():
Setup a timer that keeps on hiding the button.
NSTimer oTimer = NSTimer.CreateRepeatingTimer(0.2, this.HidePrintButton);
NSRunLoop.Current.AddTimer(oTimer, NSRunLoopMode.Default);
private void HidePrintButton()
{
if(this.oNavItem == null)
{
return;
}
this.InvokeOnMainThread(
delegate {
this.oNavItem.SetRightBarButtonItems( new UIBarButtonItem[0], false );
} );
}
This searches the navigation item:
/// <summary>
/// Finds the first navigation item inside a view hierachy.
/// </summary>
/// <param name='oCurrentView'>the view to start searching from</param>
/// <param name='oItem'>will be set if the navigation item was found</param>
public static void FindNavigationItem(UIView oCurrentView, ref UINavigationItem oItem)
{
if(oItem != null || oCurrentView == null || oCurrentView.Subviews == null || oCurrentView.Subviews.Length <= 0)
{
return;
}
// Check if a UINavigationBar was found. This will contain the UINavigationItem.
if(oCurrentView is UINavigationBar)
{
UINavigationBar oBar = (UINavigationBar)oCurrentView;
if(oBar.Items != null && oBar.Items.Length > 0)
{
oItem = oBar.Items[0];
}
return;
}
// Recursively loop all sub views and keep on searching.
foreach (var oSubView in oCurrentView.Subviews)
{
FindNavigationItem(oSubView, ref oItem);
if(oItem != null)
{
break;
}
}
}

Resources