Say, In my self.navigationController.viewControllers I have n number of viewControllers. Within them, I only want to remain 2 viewControllers in my self.navigationController.viewControllers. One is at 0 index & another one is at 1 index. Here is my code.
NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
NSMutableArray *savedVCsArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [VCs count]; i++) {
UIViewController *vc = VCs[i];
if ([vc isKindOfClass:[LanguageSettingsViewController class]]) {
[savedVCsArray insertObject:vc atIndex:0];
} else if ([vc isKindOfClass:[SignInViewController class]]) {
[savedVCsArray insertObject:vc atIndex:1];
}
}
[self.navigationController setViewControllers:savedVCsArray];
NSLog(#"counts %i", [self.navigationController.viewControllers count]);
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
But, here I always get NSLog(#"counts %i", [self.navigationController.viewControllers count]); 0. Is there I am missing something? Thanks in advance. Glad if you reply.
xcode Version 8.3.2 (8E2002)
iOS 10.3
Make sure you confirm that you actually have the VCs you want:
UIViewController *langVC = nil;
UIViewController *signVC = nil;
for (UIViewController *vc in self.navigationController.viewControllers) {
if ([vc isKindOfClass:[LanguageSettingsViewController class]]) {
// [savedVCsArray insertObject:vc atIndex:0];
langVC = vc;
} else if ([vc isKindOfClass:[SignInViewController class]]) {
// [savedVCsArray insertObject:vc atIndex:1];
signVC = vc;
}
}
if (langVC == nil || signVC == nil) {
NSLog(#"Problem exists because langVC is %# / signVC is %#", langVC, signVC);
} else {
[self.navigationController setViewControllers:#[langVC, signVC] animated:YES];
}
Related
I have a table view with multi cell.
All cells are handling the click listener very well except the two last cells.
The first one only handles clicks when I press on the red highlighted part and the second does not.
Here is a screenshot. Can anyone please tell me how to know the reason for this problem knowing that all other cells are working fine.
EDIT
This is the cell element
and this is the code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
[tableView deselectRowAtIndexPath:newIndexPath animated:YES];
KYDrawerController *elDrawer = (KYDrawerController*)self.navigationController.parentViewController;
MainViewControllerObjc *viewController = [[UIStoryboard storyboardWithName:#"Forsa" bundle:nil] instantiateViewControllerWithIdentifier:#"MainController"];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:viewController];
ForsaDataManager *sharedManager = [ForsaDataManager instance];
SocialResponse *social = [sharedManager socialLinks];
DrawerItem *test = [DrawerItem new];
test = [self.dataSource objectAtIndex: newIndexPath.row];
if ([test.tag isEqualToString:#"Login"]) {
[self openLogin];
} else if ([test.tag isEqualToString:#"Logout"]) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:0 forKey:#"user-id-key"];
[defaults setObject:#"" forKey:#"user-name-key"];
[defaults setObject:#"" forKey:#"user-email-key"];
[defaults setObject:#"" forKey:#"user-profile-key"];
[defaults synchronize];
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Forsa" bundle:nil];
SplashViewController * secondViewController = (SplashViewController *)[sb instantiateViewControllerWithIdentifier:#"splash"];
[self presentViewController:secondViewController animated:YES completion:nil];
});
} else if ([test.tag isEqualToString:#"Register"]){
[self openRegister];
} else if ([test.tag isEqualToString:#"Favorite"]){
viewController.categoryId = -2;
viewController.categoryName = #"التفضيلات";
} else if ([test.tag isEqualToString:#"Facebook"]){
[self openURL:social.facebook];
} else if ([test.tag isEqualToString:#"Twitter"]){
[self openURL:social.twitter];
} else if ([test.tag isEqualToString:#"Youtube"]){
[self openURL:social.youtube];
} else if ([test.tag isEqualToString:#"Contact Us"]){
[self openContactUs];
} else if ([test.tag isEqualToString:#"About Us"]){
int tag = 56;
[self openAboutUs:(tag)];
} else if ([test.tag isEqualToString:#"Terms"]){
int tag = 57;
[self openAboutUs:(tag)];
} else if ([test.tag isEqualToString:#"Main"]){
viewController.categoryId = -1;
} else if([test.tag hasPrefix:#"Service_"]){
NSString *string = [test.tag stringByReplacingOccurrencesOfString:#"Service_" withString:#""];
int catId = [string intValue];
viewController.categoryId = catId;
viewController.categoryName = test.title;
}
elDrawer.mainViewController=navController;
[elDrawer setDrawerStateWithState:(DrawerStateClosed) animated:(YES)];
}
Since the test is in iPhone X , there is a touch area reserved instead of the home button that's removed ( bottom safeAreaLayoutGuide ) , you need to inset your table's bottom , or increase the constant value of the bottom constraint at least On iPhone-X
I am using the LeveyPopListView.
Inside the LeveyPopList View is a table containing jobs in a specific company. All is fine until I tap a job in the pop up list view and I've checked everything.
Here's my code:
NSArray* ar_filter=(NSArray*)[self.FilterDictionary objectForKey:#"sub_slots"];
NSInteger numberOfJobs = [[[[[self.FilterDictionary objectForKey:#"sub_slots"] valueForKey:#"company_group"] valueForKey:#"job_count"] objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row] intValue];
NSLog(#"NUMBER OF JOBS: %ld", (long)numberOfJobs);
NSLog(#"ARRAY FILTER: %#", ar_filter);
//MARK: for consolidated view
if([[ar_filter objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row] objectForKey:#"company_group"])
{
if(numberOfJobs > 1)
{
NSString *company_id = [[[[self.FilterDictionary objectForKey:#"sub_slots"] valueForKey:#"company_group"] valueForKey:#"company_id"] objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row];
NSString *company_name = [[[[self.FilterDictionary objectForKey:#"sub_slots"] valueForKey:#"company_group"] valueForKey:#"company_name"] objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row];
NSDictionary *specificCompany = [NSDictionary dictionaryWithObjectsAndKeys:company_id,#"company_id", nil];
if(specificCompany.count>0)
{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:specificCompany
options:0
error:&error];
if (! jsonData)
{
NSLog(#"Got an error: %#", error);
}
else
{
strJsonStringFilter = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
}
allJobsDictionary = [NSJSONSerialization JSONObjectWithData:[network getData:[NSString stringWithFormat:#"get_all_job_offers?pt_id=%#&filter=%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"pt_id"], strJsonStringFilter]] options:kNilOptions error:nil];
//this contains the jobs that are given by allJobsDictionary
jobsToDisplay=(NSArray*)[allJobsDictionary objectForKey:#"sub_slots"];
//call LeveyPopListView
LeveyPopListView *lplv = [[LeveyPopListView alloc] initWithTitle:company_name options:jobsToDisplay handler:^(NSInteger anIndex)
{
}];
lplv.delegate = self;
[lplv showInView:self.view animated:YES];
strJsonStringFilter = #"";
}
}
- (void)leveyPopListView:(LeveyPopListView *)popListView didSelectedIndex:(NSInteger)anIndex {
NSDictionary *job = (NSDictionary*)[jobsToDisplay objectAtIndex:anIndex];
// Pass data and transit to detail job view controller
[self.parentViewController performSelector:#selector(showJobDetailWith:) withObject:job];
}
-(void)showJobDetailWith:(NSDictionary *)dictionary {
// Pass data to global variable for prepareForSegue method
mapPinSelectedDictionary = dictionary;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MTJobDetailTableViewController *smsController=[storyboard instantiateViewControllerWithIdentifier:#"MTJobDetailTableViewController"];
[smsController setJobDetailDict:mapPinSelectedDictionary];
[self.navigationController pushViewController:smsController animated:YES];
}
from LeveyPopListVIew.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// tell the delegate the selection
if ([_delegate respondsToSelector:#selector(leveyPopListView:didSelectedIndex:)])
[_delegate leveyPopListView:self didSelectedIndex:[indexPath row]];
if (_handlerBlock)
_handlerBlock(indexPath.row);
// dismiss self
[self fadeOut];
}
The app crashes when this line of code:
[self.parentViewController performSelector:#selector(showJobDetailWith:) withObject:job];
is called. Can anyone help me this. Thank you.
try this
- (void)leveyPopListView:(LeveyPopListView *)popListView didSelectedIndex:(NSInteger)anIndex {
NSDictionary *job = (NSDictionary*)[jobsToDisplay objectAtIndex:anIndex];
// Pass data and transit to detail job view controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MTJobDetailTableViewController *smsController=[storyboard instantiateViewControllerWithIdentifier:#"MTJobDetailTableViewController"];
[smsController setJobDetailDict: job];
[self.navigationController pushViewController:smsController animated:YES];
}
I want to print a list of all of the app's viewcontrollers which are loaded in order to understand why am I getting a white screen and for general debugging purposes.
Please advise,
Thanks,
Asaf
You can do some recursive printout like this (I know it's not perfect, but it's a start):
static void printViewControllerRecursively(UIViewController *viewController, NSUInteger level)
{
NSMutableString *spaces = [NSMutableString stringWithCapacity:level * 3];
for (NSUInteger i = 0; i < level; ++i)
{
[spaces appendString:#" "];
}
NSLog(#"%#->%#", spaces, viewController);
if ([viewController isKindOfClass:[UITabBarController class]])
{
for (UIViewController *child in [(UITabBarController *)viewController viewControllers])
{
printViewControllerRecursively(child, level + 1);
}
}
else if ([viewController isKindOfClass:[UINavigationController class]])
{
for (UIViewController *child in [(UINavigationController *)viewController viewControllers])
{
printViewControllerRecursively(child, ++level);
}
}
}
Then just call printViewControllerRecursively([UIApplication sharedApplication].keyWindow.rootViewController, 0);
This doesn't seem like a good way to try and debug anyhitng but that being said you can use:
NSLog(#"%#", [self.navigationController viewControllers]);
or
NSLog(#"%#", [self.tabBarController viewControllers]);
etc ...
Guess it depends on how you've setup your App.
I have UISplitView with many detailViewControllers it works fine but problem is that when i from any of the detailViewController move to LoginViewController and then back then i want that UIsplitViewController should have detailView controller which is firstViewController
here is the tableView did select method for RootViewController
[self.appDelegate.splitViewController viewWillDisappear:YES];
NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
[viewControllerArray removeLastObject];
if (row == 0) {
self.firstDetailViewController=[[[FirstDetailViewController alloc] init]autorelease];
[viewControllerArray addObject:self.firstDetailViewController];
self.appDelegate.splitViewController.delegate = self.firstDetailViewController;
}
if (row == 1) {
self.secondDetailViewController=[[[SecondDetailViewController alloc]init]autorelease];
[viewControllerArray addObject:self.secondDetailViewController];
self.appDelegate.splitViewController.delegate = self.secondDetailViewController;
}
if (row == 2) {
self.myLearningViewController=[[[MyLearningViewController alloc]init]autorelease];
[viewControllerArray addObject:self.myLearningViewController];
self.appDelegate.splitViewController.delegate = self.myLearningViewController;
}
if (row == 3) {
self.communityViewController=[[[CommunityViewController alloc]init]autorelease];
[viewControllerArray addObject:self.communityViewController];
self.appDelegate.splitViewController.delegate = self.communityViewController;
}
if (row == 4) {
self.reportsViewController=[[[ReportsViewController alloc]init]autorelease];
[viewControllerArray addObject:self.reportsViewController];
self.appDelegate.splitViewController.delegate = self.reportsViewController;
}
if (row == 5) {
self.walkInViewController=[[[WalkInViewController alloc]init]autorelease];
[viewControllerArray addObject:self.walkInViewController];
//self.appDelegate.splitViewController.delegate = self.secondDetailViewController;
self.appDelegate.splitViewController.delegate = self.walkInViewController;
}
if (row == 6) {
self.postDataViewController=[[[PostDataViewController alloc]init]autorelease];
[viewControllerArray addObject:self.postDataViewController];
self.appDelegate.splitViewController.delegate = self.postDataViewController;
}
[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];
[self.appDelegate.splitViewController viewWillAppear:YES];
[viewControllerArray release];
I want when login button is clicked then it should set the detailViewController of rootViewCOntroller to firstDetailViewController
-(IBAction)loginButton{
}
back to login
appDelegate.window.rootViewController=appDelegate.self.loginviewcontroller;
[appDelegate.window makeKeyAndVisible];
login to detailviewcontroller is alreading working you
Having some difficultly with this one.
I am loading questions, answers, correct answers from a plist. All data is being read in fine and looks like below
- (void)viewDidLoad
{
[super viewDidLoad];
rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"question" ofType:#"plist"]];
currentQuestion = -1;
[self showNextQuestion];
}
-(void) showNextQuestion{
currentQuestion++;
int numItems = [rootArray count];
NSMutableArray *question = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *A = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *B = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *C = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *addimage = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *Answer = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in rootArray) {
[question addObject:[itemData objectForKey:#"question"]];
[A addObject:[itemData objectForKey:#"A"]];
[B addObject:[itemData objectForKey:#"B"]];
[C addObject:[itemData objectForKey:#"C"]];
[addimage addObject:[itemData objectForKey:#"ImageUse"]];
[Answer addObject:[itemData objectForKey:#"ANS"]];
}
self.questionasked.text = question[currentQuestion];
self.answer1.text = A[currentQuestion];
self.answer2.text = B[currentQuestion];
self.answer3.text = C[currentQuestion];
additionImage.image = [UIImage imageNamed:addimage[currentQuestion]];
self.correctAns = Answer[currentQuestion];
if(currentQuestion == 4){
[ self performSegueWithIdentifier:#"levelclear" sender:nil];// here I am performing a segue to indicate that when 4 questions are displayed it will go to this view controller
}
}
I have 3 buttons in my VC, One for each option A,B,C which all have a function
- (IBAction)SelectA:(id)sender {
if ([self.correctAns isEqualToString:#"A"]) {
currentQuestion ++;
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier:#"incorrect" sender:nil];
}
}
- (IBAction)SelectB:(id)sender {
if ([self.correctAns isEqualToString:#"B"]) {
currentQuestion ++;
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier:#"incorrect" sender:nil];
}
}
- (IBAction)SelectC:(id)sender {
if ([self.correctAns isEqualToString:#"C"]) {
currentQuestion ++;
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier:#"incorrect" sender:nil];
}
}
When I answer the question correctly it moves to the next question. However when I answer it incorrectly it goes to the incorrect VC informing of the incorrect question. This VC has a segue when the button is pressed to go back to the question VC(which I made in the storyboard). It does go back to the question VC but simply jumps back to the first question. I know it is something regarding the currentQuestion but not sure what.
Looks like when you segue to the incorrect answer you are destroying the questions view controller, and when yo go back to it, it is re created.