//TabBarController code:
self.delegate=self;
self.tabBarController.tabBar.delegate=self;
CGRect viewFrame=self.tabBar.frame;
viewFrame.origin.y -=0;![enter image description here][1]
viewFrame.origin.x -=0;
viewFrame.size.height=30;
self.tabBar.frame=viewFrame;
firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:NULL];
secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:NULL];
NSArray *twoViewControllers = [[NSArray alloc] initWithObjects:
self.firstViewController, self.secondViewController, nil];
self.viewControllers=twoViewControllers;
// ====================================================
//
// FirstViewController code in initWithNibName:
//
// To set the title of the first tabbar item:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Article view";
NSLog(#"count = %d",[self.tabBarController.tabBar.items count]);
}
return self;
}
//How can i make the first tabbar item title "Article View" to the Center without adding any //image to the tabbaritem ?
// similar to the below tabbar items screenshot.
[1]: http://i.stack.imgur.com/xBpVH.png
Thanks in advance.
Replace the initWithNibName method
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Article view";
self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
NSLog(#"count = %d",[self.tabBarController.tabBar.items count]);
}
return self;
}
self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0); this line here adjusts the position of the image for the tabBarItem in the manner :
Shift the image in x-direction '+5' and in y-direction '-5' from the
default position.
Play with UIEdgeInsetsMake and have fun. Cheers.
Related
I have a split view app, created by adding a Split View Controller to a storyboard. I have a table view that is supposed to be grouped, with the rounded corners and gray background, but it's showing up plain style. I initialize the view in the app delegate like this:
self.dateController = [[DateViewController alloc] initWithStyle: UITableViewStyleGrouped];
Where DateViewController is my custom TableViewController class. Here's the relative code in DateViewController.m:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.view setFrame: CGRectMake( 0, 260, 768, 764 )];
toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake( 0, 0, 768, 44 )];
[self.view addSubview: toolbar];
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section {
return 3;
}
What could be causing my table view not to be grouped? Thanks!
Finally found the answer! I was overriding the wrong init function. I used this code:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
}
return self;
}
And now it works!
I have a method, which is supposed to init a navigation controller and load a view controller for a side menu. This all happens within a view controller that's not attached to any other navigation controllers.
- (void)showLeftNC
{
if (leftNavCon == nil)
{
leftNavCon = [[UINavigationController alloc] init];
}
[leftNavCon setViewControllers:#[lmvc] animated:NO];
//[leftNavCon setView:lmvc.view];
[leftNavCon.view setFrame:lmvc.view.frame];
[self.view addSubview:leftNavCon.view];
[self showCenterViewWithShadow:YES withOffset:-2];
[self.view sendSubviewToBack:leftNavCon.view];
}
leftNavCon is the navigation controller
lmvc is the primary view controller
It doesn't work in this way, neither when I call initWithRootViewController:lmvc
It only has worked when I used the commented [leftNavCon setView:lmvc.view]. But even then I couldn't get the navigation controller to push any another view controllers yet.
Help please.
never mind, i worked out something, I used lmvc as a containing view, initialised the navigation controller and all the view controllers i needed inside it
in the lmvc initWithNibName
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
if (testViewController == nil)
{
testViewController = [[UIViewController alloc] init];
}
if (navCon == nil)
{
navCon = [[UINavigationController alloc] initWithRootViewController: testViewController];
}
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 748)];
[self setView:navCon.view];
[testViewController.view addSubview:table];
[testViewController.view addSubview:button];
[testViewController.view addSubview:anyControlYouNeed];
}
and then later on
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableTwo == nil)
{
tabl = [[UITableView alloc] initWithFrame:table.frame];
}
if (testViewControllerTwo == nil)
{
testViewControllerTwo = [[UIViewController alloc] init];
}
tableTwo.delegate = self;
tableTwo.dataSource = self;
[testViewControllerTwo setView:tableTwo];
[navCon pushViewController: testViewControllerTwo animated:YES];
}
Works like a charm
i need to pass data between to Views. I really can't figure out why is not working. Variable selectedRow in SecondViewController is always empty.
FirstViewController.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
SecondViewController *newView = [[SecondViewController alloc] init];
newView.selectedRow = [tablelist objectAtIndex:indexPath.row];
newView.title = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[self.navigationController pushViewController:newView animated:YES];
}
SecondViewController.h
#interface SecondViewController : UIViewController
{
NSString *title,*selectedRow;
}
#property(nonatomic,retain) NSString *title;
#property (nonatomic,retain) NSString *selectedRow;
#end
SecondViewController.m
#synthesize title,selectedRow;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 64, 320, 370)];
textView.text = selectedRow;
textView.editable = FALSE;
[self.view addSubview:textView.text];
}
return self; }
I'm struggling for hours. I really cant figure why.
Any help?
Thank you
The problem is that you are looking at the selectedRow value before you set the property. You can't set the selectedRow property until after the call to init completes.
Since your use of the property is to setup the view controller's view, you should move that code to viewDidLoad where it belongs. As a rule, you should not access self.view in the init method of a view controller.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 64, 320, 370)];
textView.text = selectedRow;
textView.editable = FALSE;
[self.view addSubview:textView.text];
}
I used savestrings to pass data to the next viewcontroller. The only thing you need to do is import the viewcontroller from where the data comes in the viewcontroller.
You can use this in the first viewcontroller
//1e savestring
NSString *saveString1 = emailfield.text;
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
[defaults1 setObject:saveString1 forKey:#"saveString1"];
[defaults1 synchronize];
ANd this in the viewcontroller where you need to load data
//First load string
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSString *loadString1 = [defaults1 objectForKey:#"saveString1"];
[emailfield setText:loadString1];
if you are using storyboard to get a viewcontroller ,you can try this.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [ UIStoryboard storyboardWithName:#"Main" bundle:nil ];
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"test" ];
vc.title = #"something";
[self.navigationController pushViewController:detailView animated:YES];
}
I want to load another view when I click on one of my UITableCell view, but none of the things that are on the xib file (associated with that view) are showing.
This is how I'm initializing the view (in the controller that generates the tableView):
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Cards";
self.detailController = [[BasicCardViewController alloc] initWithNibName: #"CardView" bundle:nil];
}
return self;
}
This is where I deal with the selection:
- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *component = self.resultsTuples[indexPath.row];
[AJAXUtils getAsyncJsonWithUrl:(NSURL *)[NSURL URLWithString:someUrl] callback:^(NSDictionary *returnjson) {
if (returnjson != nil) {
NSString *userPageLink = returnjson[#"Node"][#"SessionInfo"][#"PostingAs"][#"Key"];
self.detailController.userPageLink = userPageLink;
self.detailController.nodePage = returnjson[#"Node"][#"Key"];
NSString *selectedCard = component[#"$element"][#"Title"];
[self.detailController setDescription:component[#"element"][#"ContactCard"][#"Description"]];
[self.detailController setPageTitle:selectedCard];
self.detailController.title = selectedCard;
NSString* rating = component[#"$element"][#"Summary"][#"AverageRating"];
self.detailController.rating =(NSInteger)rating;
[self.navigationController pushViewController:self.detailController animated:YES];
}
}];
}
This is my BasicCardView code -
#implementation BasicCardViewController
#synthesize userPageLink = _userPageLink;
#synthesize nodePage = _nodePage;
- (void)viewDidLoad {
_trendingImageView.image = [UIImage imageNamed:#"trending.png"];
}
- (UILabel *)label {
return (id)self.view;
}
- (void)loadView {
self.rateView = [[RateView alloc] init];
}
- (void)setDescription:(NSString *)description {
_description = description;
_descriptionView.text = description;
}
- (void)setPageScore:(NSString *)pageScore {
_pageScore = pageScore;
_pageScoreLabel.text = pageScore;
}
- (void)setRestaurantImage:(UIImage *)restaurantImage {
_restaurantImage.image = restaurantImage;
}
- (void)setPageTitle:(NSString *)title {
_pageTitle = title;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
self.rateView.notSelectedStar =[UIImage imageNamed:#"kermit_empty.png"];
self.rateView.halfSelectedStar = [UIImage imageNamed:#"kermi_half.png"];
self.rateView.fullSelectedStar = [UIImage imageNamed:#"kermit_full.png"];
self.rateView.rating = self.rating;
self.rateView.editable = YES;
self.rateView.maxRating = 5;
self.rateView.delegate = self;
_pageTitleLabel.text = _pageTitle;
}
Why is nothing appearing when the cell is clicked?
But if I do self.view = rateView in the viewDidLoad, the rateView appears.
The view controller calls loadView method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property. And it appeared as if your self.view is nil, in loadView method, you have created self.rateView but hasn't assigned it to self.view to make it visible.
Maybe the getAsyncJsonWithUrl block called out of the main thread try, to call the pushViewController inside the mainQueue as following:
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.navigationController pushViewController:self.detailController animated:YES];
}];
It may help sometimes.
_detailController = [[BasicCardViewController alloc] initWithNibName: #"CardView" bundle:nil];
Currently I have this code that changes the default background in a TableView to a custom picture, but when the user does a search in the UISearchBar, the Search Results TableView is a plain white background. Where/how do I change it to be the same as my regular non-searched TableView?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Employee List", #"Employee List");
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
else {
{
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Cloth.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
}
}
}
return self;
}
You should be able to access the searchResults TableView through the searchDisplay Controller:
...
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Cloth.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
self.searchDisplayController.searchResultsTableView.backgroundView = tempImageView;
...