Write into a global array from another viewController - xcode4

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;

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

How to add right button on UINavigationbar in UITabbar

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?

When assigning a readonly NSString property of an object, do you still need to [copy]?

I have MyClass setup as below. If the NSString property is readonly it should already be set to copy and autorelease. Do I still need to copy it like below?
MyClass *myClass = [[MyClass alloc] init];
NSString *assignStr = [myClass.returnStr copy];
...
[myClass release];
[assignStr release];
or
MyClass *myClass = [[MyClass alloc] init];
NSString *assignStr = myClass.returnStr;
...
[myClass release];
MyClass.h
#interface MyClass : NSObject {
#private
NSString *returnStr;
}
#property (nonatomic, readonly) NSString *returnStr;
#end
MyClass.m
#implementation MyClass
#synthesize returnStr;
#end
Using the code you provided
#interface MyClass : NSObject {
#private
NSString *returnStr;
}
#property (nonatomic, readonly) NSString *returnStr;
#end
Your property setter is defined with simple assignment
So you can use both code snippet to adjust the assignStr value, it would be correct in term of memory management ; the logical one should be to use the classical assign pattern.
But in fact it is more about, how you are handling the master value (the value you are returning as read-only) and what you want to achieve with it (sorry this is not that clear to me in the provided sample, which does not seems to match 100% your thread title)

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