How to add right button on UINavigationbar in UITabbar - uinavigationcontroller

I have a problem when add a right button on uinavigationbar. My Nib organizer like this:
CustomTabViewController :UIViewController
-->CustomTableViewController1 : UIViewController
-->CustomTableViewController2 : UIViewController
CustomTabViewControllers are in Tabbar items
I need to save the inputs, I can add navigation item from Navigation Controller, but I can't add them from View Controller when they are childs in Tabbar items.
here is my code:
#interface CustomTabViewController : UIViewController <UITabBarDelegate> {
NSArray *detailViews;
IBOutlet UITabBar *tabBar;
IBOutlet UITabBarItem *tabBarItem1;
IBOutlet UITabBarItem *tabBarItem2;
UIViewController *selectedViewController;
}
#property (nonatomic, retain) NSArray *detailViews;
#property (nonatomic, retain) IBOutlet UITabBar *tabBar;
#property (nonatomic, retain) IBOutlet UITabBarItem *tabBarItem1;
#property (nonatomic, retain) IBOutlet UITabBarItem *tabBarItem2;
#property (nonatomic, retain) UIViewController *selectedViewController;
#end
#implementation TabViewController
#synthesize detailViews;
#synthesize tabBar;
#synthesize tabBarItem1;
#synthesize tabBarItem2;
#synthesize selectedViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil number:(NSString *)num{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
CustomTableViewController1 *tVC1 = [[CustomTableViewController1 alloc]initWithNibName:#"CustomTableViewController1" bundle:nil];
CustomTableViewController2 *tVC2 = [[CustomTableViewController2 alloc]initWithNibName:#"CustomTableViewController2" bundle:nil];
NSDictionary *detail = [[[Services sharedInstance] getDetail:num] copy];
[tVC1 setDetail:detail];
[tVC2 setDetail:detail];
NSArray *array = [[NSArray alloc] initWithObjects:tVC1, tVC2, nil];
self.detailViews = array;
[self.view addSubview:tVC1.view];
self.selectedViewController = tVC1;
[array release];
[TVC release];
[TVC release];
[detail autorelease];
}
return self;
}
// ....
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item == tabBarItem1) {
CustomTableViewController1 *tVC1 = [detailViews objectAtIndex:0];
[self.selectedViewController.view removeFromSuperview];
[self.view addSubview:tVC1.view];
self.selectedViewController = tVC1;
} else if (item == tabBarItem2) {
CustomTableViewController2 *tVC2 = [detailViews objectAtIndex:1];
[self.selectedViewController.view removeFromSuperview];
[self.view addSubview:tVC2.view];
self.selectedViewController = tVC2;
}
}
#end
the Custom Table View Controllers are inside the tab bars with some default value which need to be change by user and save. I can add a right button on uinavigationbar from my TabViewController class but it can not rich the information inside CustomTableViewController in tab bars. Is there any way to add a right button on uinavigationbar from Custom Table View Controller which I have? Can you show me the solution?

Related

UISearchController SearchBar Overlapping Status Bar and Whitespace

I have a view controller with a search button as the navigation item's right bar button item. When the search button is pressed, I want the a UISearchController to be presented. Here is the relevant code:
#interface BaseViewController ()
#property (nonatomic, strong) UISearchController *searchController;
#property (nonatomic, strong) UIBarButtonItem *searchBarButtonItem;
#property (nonatomic, strong) UIBarButtonItem *accountBarButtonItem;
#property (nonatomic, strong) UITableViewController *searchResultsController;
#end
#implementation BaseViewController
- (instancetype)init {
if (self = [super init]) {
UIImage *accountIcon = [UIImage imageNamed:#"account-male-icon#2x.png"];
UIBarButtonItem *accountBarButtonItem = [[UIBarButtonItem alloc] initWithImage:accountIcon style:UIBarButtonItemStylePlain target:self action:#selector(accountButtonPressed:)];
UIBarButtonItem *searchBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchButtonPressed:)];
[self setSearchResultsController:[[UITableViewController alloc] init]];
[self setSearchController:[[UISearchController alloc] initWithSearchResultsController:[self searchResultsController]]];
[[[self searchResultsController] tableView] setDataSource:self];
[[[self searchResultsController] tableView] setDelegate:self];
[[[self searchResultsController] tableView] setTableHeaderView:[[self searchController] searchBar]];
[[self searchController] setSearchResultsUpdater:self];
[[self searchController] setDelegate:self];
[[self searchController] setHidesNavigationBarDuringPresentation:YES];
[[self searchController] setDimsBackgroundDuringPresentation:YES];
[[[self searchController] searchBar] setDelegate:self];
[self setDefinesPresentationContext:YES];
[self setSearchBarButtonItem:searchBarButtonItem];
[self setAccountBarButtonItem:accountBarButtonItem];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationItem] setLeftBarButtonItem:[self accountBarButtonItem]];
[[self navigationItem] setRightBarButtonItem:[self searchBarButtonItem]];
}
- (IBAction)searchButtonPressed:(id)sender {
[self presentViewController:[self searchController] animated:YES completion:nil];
}
When the search button is pressed, the searchController gets presented. All is well, except the following two things occur:
1.) The search bar overlaps the status bar
2.) There is white space in the first row of the table view
Below are pictures illustrating the problem:
How do I resolve the status bar issue and remove the white space in the first row?

NSPopUpButtonCell error when embedded in an NSTableColumn that is bound

When binding cells in my NSTableView of type NSPopUpButtonCell I got an error
[<NSTableColumn > valueForUndefinedKey:]: this class is not key value coding-compliant for the key value.
The reality is that I am simply after the contents of the NSPopUpButtonCell as a string
But i changed
NSString *name;
to
NSObject *name;
Which made my application LOAD but crash when trying to display the contents to a NSTableView that displays the NSPopUpButtonCell column
ERROR: unrecognized selector sent to instance
When I have an UNBOUND cell, i am not getting an error, further if the cell is type NSTextFieldCell i do not have a problem and can use the NSString class instead.
I am guessing that this is about connecting the right bits to everything, from what I can see on the error, So, how do i have the popup cell to display the popup button but save the value of the selected sell in the actual array?
Thanks
//------------------------------------------------
//my.h
//------------------------------------------------
#interface theItemsInArrayC : NSObject {
#private
NSString *name;
int age;
}
#property int age;
#property (copy) NSString *name;
#end
#interface mybigList : NSObject {
#private
NSMutableArray *theItems;
}
#property (copy) NSMutableArray *theItems;
#end
//------------------------------------------------
AND
//------------------------------------------------
//my.m
//------------------------------------------------
#implementation theItemsInArrayC
#synthesize name;
#synthesize age;
- (id)init
{
self = [super init];
if (self) {
age = 19;
name = #"Another Name";
}
return self;
}
#end
#implementation mybigList
#synthesize theItems;
- (id)init
{
self = [super init];
if (self) {
theItems = [[NSMutableArray alloc] init];
}
return self;
}
#end
//------------------------------------------------
I found the sample code for what i wanted to do at https://github.com/johnjohndoe/NSPopUpButtonCell
i hope this helps the next person!
and also a big thank you to that person on github toooooooo

iOS: Pointer get lost when poping back to parentView

I code for iOS 6.1 with ARC and have a problem that my objects in the nsmutablearray get "lost".
I will explain it in more detail:
There is a parent view which has a NSMutableArray which should hold some objects(addresses)
Everytime I hit the button the data of the Array will be displayed with NSLog and then the navigation controller pushes to the ChildView
The childview generates a new address object
If I do this and then press the back button in the view and wants to try the same case (again pressing the button) the data in the array is lost and I get a EXC_BAD_ACCESS
My assumption: When I go back to parentView ARC deallocates the second view and everything in it. But my Address object will be referenced from the array so that the ARC counter for this object should not be 0.
Can anyone help me?
Here is the concrete code
ParentViews Controller h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) NSMutableArray *adresses;
#property int count;
-(void)goFurther;
#end
ParentViews Controller m:
#import "ViewController.h"
#import "SecondViewController.h"
#import "Address.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
self.title=#"First View";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"Add address" style:UIBarButtonItemStylePlain
target:self action:#selector(goFurther)];
[self.navigationItem setLeftBarButtonItem:addButton];
self.adresses=[[NSMutableArray alloc] init];
self.count=0;
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) goFurther{
for (Address *a in self.adresses) {
NSLog(#"Adresse:");
NSLog(a.street);
NSLog(a.door);
}
self.count++;
SecondViewController *second=[[SecondViewController alloc]initWithView:self];
[self.navigationController pushViewController:second animated:true];
}
#end
ChildViews Controller h:
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface SecondViewController : UIViewController
#property ViewController * viewCon;
-(id) initWithView: (ViewController*) view;
#end
ChildViews Controller m:
#import "SecondViewController.h"
#import "Address.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(id) initWithView: (ViewController*) view{
self = [super init];
self.viewCon=view;
Address *a=[Address alloc];
a.street=[NSString stringWithFormat:#"Street %d", self.viewCon.count];
a.door=#"Door";
[self.viewCon.adresses addObject: a];
return self;
}
- (void)viewDidLoad
{
self.title=#"Second View";
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You have to send your data back to the parent view before dismissing the child view.
Declare a #protocol (say, called ChildControllerDelegate) in your child controller's .h file with a method didDismissWithData: with some data type that is suitable for you (maybe string or dictionary). Have the parent view controller conform to the protocol. Create a #property of the child called delegate of type id<ChildControllerDelegate>
Then before dismissing the child, call
[self.delegate didDismissWithData:newData];
In the parent controller, handle this message and save your new address.

Write into a global array from another viewController

I have an array in a viewController and I want to write into that array from another viewController, so I defined that array as global but I didn't manage to write into it!
ViewController1.h
extern NSArray *xArray;
#property (nonatomic, retain) NSArray *xArray;
ViewController1.m
#synthesize xArray;
NSArray *xArray;
xArray = [[NSArray alloc] init];
ViewController2.m
#import "ViewController1.h"
NSArray *zArray = [NSArray arrayWithObjects:#"a",#"b",nil];
ViewController1.xArray = zArray;
I'm getting the following error:
Property 'xArray' not found on object of type 'ViewController1'
Found the solution, define the array in app delegate and use it in any class
AppDelegate.h
NSArray *xArray;
..
#property (nonatomic, retain) NSArray *xArray;
..
#synthesize *xArray;
ViewController1.m
NSArray *tmpArray1;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray1;
ViewController2.m
NSArray *tmpArray2;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray2;

UILabel to NSString to load webview

So I have this code, where I am sending a variable storyLink to a label and display it in the specified nib. So far the label displays the text which is what I want. But I want to use that text as an url for a webview. urlString = nothing as of right now because I can't figure out how to use/convert an UILabel to a String suitable for a webview url. So if that was confusing basically I want to take the text stored in my label and use it for the url in my webview. Can someone help?
TestView.h
#interface TestView : UIViewController {
IBOutlet UILabel *label;
IBOutlet UIWebView *webView;
NSString *urlString;
}
#property(nonatomic, retain)IBOutlet UILabel *label;
#property(nonatomic, retain)IBOutlet UIWebView *webView;
#property(nonatomic, retain)NSString *urlString;
#end
TestView.m
- (void)viewDidLoad {
[super viewDidLoad];
label.text=nil;
urlString=something;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
sending code
TestView *objFirstController=[[[TestView alloc] initWithNibName:#"TestView" bundle:nil] autorelease];
[self.navigationController pushViewController:objFirstController animated:YES];
objFirstController.label.text=storyLink;
When setting properties that are accessed during 'viewDidLoad', make sure to put them before you use pushViewController.
TestView.m
- (void)viewDidLoad
{
[super viewDidLoad];
label.text=urlString;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
sending code
TestView *objFirstController = [[[TestView alloc] initWithNibName:#"TestView" bundle:nil] autorelease];
objFirstController.urlString = storyLink;
[self.navigationController pushViewController:objFirstController animated:YES];

Resources