I have seen several related questions, but mine is different in that I want to do it purely in coding, so without storyboards or interface builder.
Basically I get lost in how to properly display the cells in a TableView. I have created a very simple SplitView test application. In it I try to show the same TableView twice: once in the TableViewController and once in the DetailViewController (for a user this would make little sense, but I try to grasp how to get this done).
In the TableViewController, the 3 cells show correctly, but in the DetailView, the TableView shows without the cells. There must be a simple step to have the cells drawn, but all the example cases I find assume you use storyboards or IB
In the SplitView I have:
- (id) init{
self = [super init];
JBDetailViewController *detailVC = [JBDetailViewController controller];
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detailVC];
JBTableViewController *rootVC2 = [[JBTableViewController alloc] init];
rootVC2.title = #"A TableVC title";
UINavigationController *rootNav2 = [[UINavigationController alloc] initWithRootViewController:rootVC2];
self.viewControllers = #[rootNav2, detailNav];
self.delegate = detailVC;
self.tabBarItem.title = #"a Tab title";
return self;
}
In the DetailViewer I have:
- (void) loadView {
[super loadView];
self.view.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.85f];
JBTableViewController *rootVC2 = [[JBTableViewController alloc] init];
[rootVC2 loadView];
rootVC2.title = #"Test van Justus";
rootVC2.view.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.85f];
[self.view addSubview:rootVC2.view];
}
Both use the same JBTableViewController that is very simple and should display 3 cells, which it does in the VC, but not in the DetailView.
What do I do wrong?
Related
In my view controller I am adding a search bar programmatically and it appears exactly the way i want but when i click on it do nothing i can't write/type anything on the search bar, And there is no error on the log. I added search bar in another view controller same way,it does works but in that particular it doesn't work. I am pushing this view controller from previous view controller so it has a navigation back button on the left (check the image) it works too.
In the view controller there is a TableView and a CollectionView i want to add the search for the table view. I was wondering if there is anything to do with having both of this in the same view controller.
Code to add search bar
- (void)viewDidLoad {
//other stuff on view controller
[self setSearchView];
}
-(void)setSearchView{
//searchController is added as a property on .h file
//and all the delegate are also added
//#property (strong, nonatomic) UISearchController *searchController;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater= self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.navigationController.toolbarHidden=YES;
self.navigationItem.titleView=self.searchController.searchBar;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;
}
I am coming to this view controller from a previous view controller and pushing this view controller when a button is clicked .And i am using nib file
Button action
- (void)ButtonClicked {
MYNextViewController *nextViewController = [[MYNextViewController alloc] initWithNibName:#"MYNibFileName" bundle:nil];
// I also have a tab bar on the bottom which i am hiding for that view controller when pushing
nextViewController .hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:nextViewController animated:YES];
}
Image:
Any help will be greatly appreciated. Hope i made my self clear...
Finally i was able to solve this problem with some change in view hierarchy thanks to #Tj3n , but not sure if this is the correct solution but right now its working ..
I also had to remove this line and add few more codes for other purpose
self.definesPresentationContext = YES; //removed...
also answer of that questions was helpful
I had a similar issue and tried the answers here, but it didnt work for me. Then stumbled upon this post https://forums.developer.apple.com/thread/122972
Basically for me disabling Automatically Sync Pasteboard worked. (Simulator -> Edit -> Automatically Sync Pasteboard)
//Please use this code
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.navigationController.navigationBar.bounds.size.height)];
_searchBar.tintColor = [UIColor blueColor];
//[_searchBar setImage:[UIImage imageNamed:#"cross_icon"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
_searchBar.placeholder = #"Enter drug name";
//_searchBar.showsBookmarkButton = YES;
_searchBar.barTintColor = [UIColor clearColor];
UITextField *txfSearchField = [_searchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor whiteColor];
txfSearchField.tintColor = [UIColor blackColor];
txfSearchField.textColor = [UIColor blackColor];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
_searchBar.showsCancelButton = NO;
[_searchBar becomeFirstResponder];
[self.navigationController.navigationBar addSubview:_searchBar];
For your particular case, Use this.
self.searchController.obscuresBackgroundDuringPresentation = NO;
This will allow you to use the same tableview to interact with search result.
Programmatically created segue crashes the app on performSegueWithIdentifier:, I really don't want to use the storyboard though.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
ViewController *viewController = [[ViewController alloc] init];
self.segue = [[UIStoryboardSegue alloc] initWithIdentifier:#"showInfo" source:self destination:viewController];
//change the background color to white
self.view.backgroundColor = [UIColor whiteColor];
//create the table view
UITableView *tableView = [[UITableView alloc] init];
//initialize the data source and the delegate to self - as the methods are going to be specified in this script
tableView.dataSource = self;
tableView.delegate = self;
//register the class for the tableView
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cell"];
//now assign the table view to our our viewController's property
self.view = tableView;}
Segues can't be created programmatically. Apple's documentation says:
You do not create segue objects directly. Instead, the storyboard runtime creates them when it must perform a segue between two view controllers.
The initWithIdentifier:source:destination: method is for subclassing purposes.
That said, if you're not using storyboards you don't really need segues anyway. Just instantiate and present the destination view controller when you need to.
I have recently removed the tab bar from my app in favor of a "slide out" styled menu which I have written myself. It appears to be working perfectly, except that the one split-view controller I use in the app does not work correctly on the iPad in portrait orientation (landscape is just fine). The problem I'm seeing is the detail VC ONLY is loaded into the portrait view, where both VCs load correctly in landscape view. This is on iOS7.
So, here is the code I'm using. This code has been moved from the AppDelegate (previously) to the root VC. There were some changes but relatively minor. It did work fine with the tab bar, but using this new slide out menu (i.e., a table view) I have this one problem. I'm only including the portions of the code that I think would be relevant.
AdminMasterViewController *adminMasterVC = [[AdminMasterViewController alloc] init];
UINavigationController *adminMasterNav = [[UINavigationController alloc] init];
adminMasterNav.viewControllers = [NSArray arrayWithObjects:adminMasterVC, nil];
adminMasterNav.view.frame = CGRectMake(0,0,[Utility screenWidth],[Utility screenHeight]);
AdminDetailViewController *adminDetailVC = [[AdminDetailViewController alloc] init];
UINavigationController *adminDetailNav = [[UINavigationController alloc] init];
adminDetailNav.viewControllers = [NSArray arrayWithObjects:adminDetailVC, nil];
adminDetailNav.view.frame = CGRectMake(0,0,[Utility screenWidth],[Utility screenHeight]);
UISplitViewController *adminSplitVC = [[UISplitViewController alloc] init];
adminSplitVC.viewControllers = [NSArray arrayWithObjects: adminMasterNav, adminDetailNav, nil];
adminSplitVC.delegate = self;
adminSplitVC.title = #"Admin";
adminSplitVC.view.frame = CGRectMake(0,0,[Utility screenWidth],[Utility screenHeight]);
vcArray = [NSArray arrayWithObjects:homeVC, adminSplitVC, expressiveNav, receptiveNav, typerNav, nil];
(The last line builds the array of all the VCs in the project; the code which creates these VCs has been omitted).
After the VC has been selected it is presented as a child VC as follows:
UIViewController *vc;
if ([selected isEqualToString:#"Home"])
{
vc = [vcArray objectAtIndex:VCHome];
}
else if ([selected isEqualToString:#"Administrator"])
{
vc = [vcArray objectAtIndex:VCAdmin];
}
... (others listed here)
[self.view addSubview:vc.view];
[self addChildViewController:vc];
[vc didMoveToParentViewController:self];
From what I have read there may be some issue with NOT using the tab bar, but the articles are confusing because a lot of them are dated. I will appreciate any input or suggestions on this problem as I've read everything I can find and don't really know where to go from here. TIA.
After a couple of days of reading and trying different things, an hours after posting the question I solved this. Just in case anyone else hits this same problem, here's the solution:
I subclassed the UISplitViewController and made it its own delegate.
Then, this delegate method solved it:
- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
return NO;
}
I have looked at all of the similar/related questions, but none either a) are exactly my problem or 2) the solutions just don't work.
In my appDelegate.m I have in didFinishLaunchingWithOptions
JCGRootNavigationController *rnc = [[JCGRootNavigationController alloc] init];
self.window.rootViewController = rnc;`
JCGRootNavigationController is a subclass of UINavigationController
#interface JCGRootNavigationController : UINavigationController`
In JCGRootNavigationController.m:
#implementation JCGRootNavigationController
-(instancetype) init {
self = [super init];
self.view.backgroundColor = [UIColor lightGrayColor];
self.navigationItem.title = #"MY TITLE";
return self;
}
And the title just won't display. I see the Navigation Bar, but no title. Looks like lots of people over the years have had this same problem. Maybe a simple answer will help clear up all of the confusing. This is so incredibly frustrating.
When working with a Storyboard, setting the title of the UIViewController or UITableViewController does not seem to add that title to the Navigation Controller, as other answers suggest.
Instead, find the UINavigationItem that is likely alongside your View Controller object in the Storyboard hierarchy. Set this Navigation Item's title to apply that title to the Navigation Controller.
UINavigationController automatically shows the title of the UIViewController subclass it is displaying. It does so by looking at the navigationItem.title property of that UIViewController or UIViewController subclass. Basically UINavigationController doesn't have a title.
Thanks believesInSanta, While I cannot find this explicitly stated in apple documentation anywhere, I have to go with this being the answer. UINavigationController doesn't have a title.
To get the title to work, back in appDelegate.h I added:
JCGTableViewController *tvc = [[JCGTableViewController alloc] init];
JCGRootNavigationController *rnc = [[JCGRootNavigationController alloc] initWithRootViewController:tvc];
self.window.rootViewController = rnc;
Where JCGTableViewController is another subclass I added. As you can probably tell, it is a subclass of UITableViewController.
In JCGTableViewController I overrode init to:
-(instancetype) init {
self = [super init];
if(self) {
self.title = #"TVC";
self.view.backgroundColor = [UIColor lightGrayColor];
}
return self;
}
While I used a tableViewController, I imagine you can add any view to the NavigationController and set the properties that way. I will play around with that today.
Thanks all!
I have a requirement where I need to slide the view but thats inside a UITabBarController.
Does any one tried doing that ? ECSlidingViewController works perfect if I make it rootViewController but I want that in UITabBarController.
I tried setting the ECSlidingViewController as one of the view controllers for the UITabBarController but it keeps crashing after the first swipe or some times it goes in infinite loop
Below is my code
myWorkController = [[TaskWorkViewController alloc] initWithNibName:#"WorkViewController_iPhone" bundle:nil]; // This is the controller where I have my specific logic
myWorkNavController = [[[UINavigationController alloc] initWithRootViewController:myWorkController] autorelease]; // setting the myWorkController to UINavigationController as I need to navigate from this view controller to different view on specific actions
//SlidingviewController setup
self.slidingViewController = [ECSlidingViewController slidingWithTopViewController:myWorkNavController];
self.slidingViewController.topViewController = myWorkNavController;
[myWorkNavController.view addGestureRecognizer:self.slidingViewController.panGesture];
self.slidingViewController.anchorRightPeekAmount = 100.0;
myWorkController.slidingViewController = self.slidingViewController;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = #[ self.slidingViewController, createNavController, currentWorkNavController];
and I am setting the underRightViewController inside myWorkController
if (self.slidingViewController != nil)
{
UIViewController *underRightViewController = [[[UIViewController alloc] init] autorelease];
// configure under right view controller
underRightViewController.view.layer.borderWidth = 0;
underRightViewController.view.layer.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor;
underRightViewController.view.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
underRightViewController.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeBottom | UIRectEdgeRight; // don't go under the top view
self.slidingViewController.underRightViewController = underRightViewController;
}
1) First swipe works perfect but the second swipe breaks in ECSlidingViewController code
2) As I am using UINavigationController, when I try to change underRightViewController it fails at [oldUnderRightViewController removeFromParentViewController]; of ECSlidingViewController code
Can some one please guide me what I am doing wrong or if this is possible with ECSlidingViewController
Thanks in advance