Data for UITableView gets changed - iphone

I'm trying to create a Settings icon for the rightBarButtonItem for my UINavigationController. In my application:DidFinishLaunching, I create the button, and set it
//pseudo code for applicationdidfinish launching
HomeController *home = [[HomeController alloc] init]; // root view of my UINavigationController
home.navigationItem.rightBarButtonItem = settingsBarButtonItem;
[settingsButton addTarget:self action:#selector(settingsPressed:) forControlEvents:UIcontrolEventTouchUpInside]; // i used a button for the barbuttonitem to not get the bar button item border
than in settingsPressed:
SettingsController* settings = [[SettingsController alloc] initWithNibName:#"SettingsController" bundle:nil];
UINavigationController* popoverNav = [[UINavigationController alloc] initWithRootViewController:settings];
[settings release];
popover = [[UIPopoverController alloc] initWithContentViewController:popoverNav];
[popoverNav release];
// show settings
if ([popover isPopoverVisible]) {
[popover dismissPopoverAnimated:YES];
} else {
[popover presentPopoverFromBarButtonItem:bbiSettings permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
in SettingsController, in viewDidLoad:
NSArray *array = [NSArray arrayWithObjects:#"one", #"two", #"three", nil];
self.DataArray = array; // (nonatomic, retain)
[array release];
I do not show this array right away. Like the iPhone Settings app, when they click on one of the cells in my Grouped Table, it opens up a new UITableView. So in that UITableView, in the tableView:numberOfRowsInSection: method, I
return [self.DataArray count];
However, it is here that my app crashes. When I look at my array, I now have random stuff in there, like vl.proj sometimes, UIViews, etc. I do not know why this array gets changed. I do not know if it's because I'm calling the popover from the applicaionDelegate which I normally do not do and that is the problem, or if there is something else wrong. Thanks.

[NSArray arrayWithObjects:] returns an autoreleased array. So you don't have to release it manually. You can just remove the line [array release]; in viewDidLoad and everything should work fine.

Related

positionig view in didFinishLaunchingWithOptions

I am searching for 4 days but can't reach any solution.
In my iOS app i am trying to use push notifications and there is no problem there. I receive notification and in didReceiveRemoteNotification method with using following code i can reach detail view with no problem.
NSDictionary *aps = [userInfo objectForKey:#"aps"];
NSArray *array = [[aps objectForKey:#"acme"] componentsSeparatedByString:#","];
NSArray *fixtureArray = [[CreateFixture alloc] createFixtureWithArray:array andStyle:#"single"];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
UIViewController *root = navController.topViewController;
FPViewController *vc = [[FPViewController alloc] init];
[vc createViewWithArray:fixtureArray];
NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil];
[navController setViewControllers:vcs animated:YES];
When i close the app completely and send notification, my app opens with didFinishLaunchingWithOptions as expected. I am using same code in following if statement:
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
same code above
}
}
BUT!!! this time my detail view comes up with different x y and width height. So the app become useless. some labels and buttons not seen some view bigger and over some other views.
İ can't find any solution PLEASE HELP! (All the views, labels, buttons etc. are created programmatically) i am using autoresizingMask for landscape and portrait window. And i am open for any suggestion. Thank you for help.
i think overriding the drawRect method solves this problem.and set frames in drawRect method
What is the difference i don't know but changing the push detail view code solve the problem.
Here is my old code
NSDictionary *aps = [userInfo objectForKey:#"aps"];
NSArray *array = [[aps objectForKey:#"acme"] componentsSeparatedByString:#","];
NSArray *fixtureArray = [[CreateFixture alloc] createFixtureWithArray:array andStyle:#"single"];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
UIViewController *root = navController.topViewController; //delete
FPViewController *vc = [[FPViewController alloc] init];
[vc createViewWithArray:fixtureArray];
NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil]; //delete
[navController setViewControllers:vcs animated:YES]; //delete
And i delete root viewcontroller and setViewControllers: method.
and added pushViewController: animated: method.
[navController pushViewController:vc animated:YES];
hope some one with same problem don't loose 5 days. Good luck all.

Populate Table With Help of PopOverController - Objective C

I am using a UIButton, on clicking it I want to display the contents that are present in my NSMutableArray in UITableView with the help of UIPopOverController i.e. I want a UITableView to pop up whose cells show the contents of my NSMutableArray.
I am using the following lines of code:
UITableViewController *table= [[UITableViewController alloc]init];
table.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:table];
self.popoverController = popover;
popoverController.delegate = self;
NSString *hDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *hFilePath = [hisDir stringByAppendingPathComponent:#"hhhh.txt"];
NSArray *array = [NSArray arrayWithContentsOfFile:hFilePath ];
NSMutableArray *kkkk = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
[kkkk addObjectsFromArray:[dict allKeys]];
}
table = [[UIImageView alloc] initWithFrame:[window bounds]];
// Set up the image view and add it to the view but make it hidden
[window addSubview:table];
table.hidden = YES;
[window makeKeyAndVisible];
I am unable to get the UITableView to pop up on the press of my UIButton. Can anyone help me to sort it out?
One way to show the UITableView in the UIPopOverController is by creating a new UIViewController class. And invoking it in initWithContentViewController method of UIPopOverController.
1. Create a new UIViewController or UITableViewController class. Create an instance of it.
2. Use the instance in the initWithContentViewController method of UIPopOverController.
3. Mention from where it should "pop"
For Example in your Button action :
-(IBAction)yourButtonAction:(id)sender
{
YourNewViewController*newVC=[[YourNewViewController alloc]init];
UIPopoverController*somePopOver=[[UIPopoverController alloc]initWithContentViewController:catergoryVC]; //Tell which view controller should be shown
[somePopOver setPopoverContentSize:CGSizeMake(200, 200)]; // set content size of popover
[somePopOver presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; //From where it should "pop"
}
It seems you want to present it from a UIBarButtonItem so instead of presentPopoverFromRect use presentPopoverFromBarButtonItem
[somePopOver presentPopoverFromBarButtonItem:yourBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
If you want to display popover on press of button click, then first add your button in viewcontroller, display that view controller as follows:
In app delegate write code:
MyViewController *viewController = [[MyViewController alloc] init];
[self.window addSubView:viewController.view];
In MyViewController add button and provide target to that button displayPopup as follows:
-(void)displayPopup:(id)sender
{
UITableViewController *tblViewPopover = [[UITableViewController alloc] init];
tblViewPopover.tableView.delegate = self;
tblViewPopover.tableView.dataSource = self;
tblViewPopover.tableView.backgroundColor = [UIColor whiteColor];
tblViewPopover.tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
float theWidth = 280;
tblViewPopover.contentSizeForViewInPopover = CGSizeMake(theWidth,200);
if(m_popOvrController){
[m_popOvrController dismissPopoverAnimated:NO];
[m_popOvrController release];
m_popOvrController=nil;
}
m_popOvrController = [[UIPopoverController alloc] initWithContentViewController:tblViewPopover];
[tblViewPopover release];
[m_popOvrController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
and you can use tableview delegate methods to display data in tableview.
I think, UIPopoverViewController is initialized using UIViewController and here you are using UIView(UITableView).
Can you please try using UITableViewController instead?
Also, if things does not work according to plan when you create it using the code try using an XIB explicitely.
This should help.

How to Display title for uitabbarcontroller

I have created uitabbarview controller using the below code
In the uitabbar controller i want to display tile for every view
Please let me know how to add title for each view ( tab bar item )
myTabBarController = [[UITabBarController alloc] init];
MyDialerViewController *aDialerViewController = [[MyDialerViewController alloc]init];
MyCallLogViewController *aCallLogViewController = [[MyCallLogViewController alloc] init];
TemplateViewController *aTemplateViewController = [[TemplateViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:aDialerViewController, aCallLogViewController, aTemplateViewController, nil];
myTabBarController.viewControllers = controllers;
myTabBarController.delegate = self;
myTabBarController.selectedIndex = 0;
[controllers release];
[aDialerViewController release];
[aCallLogViewController release];
[aTemplateViewController release];
[self.window addSubview:myTabBarController.view];
[self.window makeKeyAndVisible];
I set the title of the navcontroller directly in the appDelegate.
aDialerViewController.title = NSLocalizedString(#"Dialer Title", #"Dialer Title");
aCallLogViewController.title = #"Title";
aTemplateViewController.title = #"Title";
I'd also set it in the viewDidLoad method of those viewControllers.
Works fine for me. Remember to localize, just incase you need it in the future. (if not intended to, you should look into it)
self.viewcontroller.tabBarItem = // a UITabBarItem instance
Make an instance of UITabBarItem
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITabBarItem_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UITabBarItem
(id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag
You could write that in the initializer function of each viewcontroller.

Can't get Secondary UITableViewController to display inside a UITabBarController

I've programmatically created a UITabBarController that is loaded in my App Delegate like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
myTableViewController = [[MyTableViewController alloc] init];
UINavigationController *tableNavController = [[[UINavigationController alloc] initWithRootViewController:myTableViewController] autorelease];
myTableViewController.title = #"Tab 1";
[myTableViewController release];
mySecondTableViewController = [[MySecondTableViewController alloc] init];
UINavigationController *table2NavController = [[[UINavigationController alloc] initWithRootViewController:mySecondTableViewController] autorelease];
mySecondTableViewController.title = #"Tab 2";
[mySecondTableViewController release];
tabBarController.viewControllers = [NSArray arrayWithObjects:tableNavController, table2NavController, nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
Now the issue I have is that I can get into the views no problem, but when I try and click onto any item in the Table View, I can't get a secondary table view to appear in any tab. The tabs work absolutely fine, just the secondary views. I'm using the code below in my myTableViewController to run when selecting a specific row (the code reaches the HELP line, and crashes)...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
// this gets the correct view controller from a list of controllers
SecondaryViewController *svc = [self.controllers objectAtIndex:row];
/*** HELP NEEDED WITH THIS LINE ***/
[self.navigationController pushViewController:svc animated:YES];
}
Simply put, I'm trying to switch views to the new view controller whilst keeping the tabs available and using the navigation to go back and forth (like in the iTunes App).
Simply put, I was creating the controller array, self.controllers, and adding objects, and then releasing the objects.
If you do not release the object until the array is released, it appears to work no problem.
You've not included in your question how you initialize the self.controllers array.
I suspect this array is not filled with initialized SecondaryViewController objects.
EDIT (Added Code example that works for me):
the .h file:
#interface FirstLevelViewController : UITableViewController {
NSArray *controllers;
}
#property (nonatomic, retain) NSArray *controllers;
#end
and the .m file:
#implementation FirstLevelViewController
#synthesize controllers;
- (void)viewDidLoad {
self.title = #"First Level";
NSMutableArray *array = [[NSMutableArray alloc] init];
// Disclosure Button
DisclosureButtonController *disclosureButtonController =
[[DisclosureButtonController alloc]
initWithStyle:UITableViewStylePlain];
disclosureButtonController.title = #"Disclosure Buttons";
disclosureButtonController.rowImage = [UIImage
imageNamed:#"disclosureButtonControllerIcon.png"];
[array addObject:disclosureButtonController];
[disclosureButtonController release];
// deleted further adds to array ...
self.controllers = array;
[array release];
[super viewDidLoad];
}

Initial selected Item in an UITabBar without a UITabBarController

How do I set the default selected UITabBarItem in an UITabBar that is within an UIView, not an UITabBarController?
Just to clarify, the UIView does implement the protocol and the didSelectItem method works. At run-time, the tabbar works and the tabbaritems selected when the user touches them. My problem is setting the default selected item.
If i use [myTabbar setSelectedItem] within the didSelectItem method it works. But outside of it, it doesn't (for example, in the viewDidLoad method of my UIView).
Thanks!
Thank you for your approatch, but I still have a problem that the didSelectedItem is not called when I select the item with setSelectedItem. Any Idea?
actually, I did use it a little bit different:
[tabbar setSelectedItem:[tabbar.items objectAtIndex:0]];
finally I solved it this way....
- (void)viewDidLoad
{
UITabBarItem *item = [myTabBar.items objectAtIndex:0];
[self.myTabBar setSelectedItem:item];
if(tab1Exam == nil){
self.tab1Exam = [[CurExam alloc] initWithNibName:#"CurExam" bundle:nil];
[self.view insertSubview:tab1Exam.view belowSubview:myTabBar];
if (currentViewController != nil)
[currentViewController.view removeFromSuperview];
currentViewController = tab1Exam;
}
}
You might want to post some sample code. I just ran a quick test in -viewDidLoad:
UITabBarItem *about = [[UITabBarItem alloc] initWithTitle:#"About" image:[UIImage imageNamed:#"About.png"] tag:0];
NSArray *tabBarItems = [[NSArray alloc] initWithObjects:about,nil];
[tabBar setItems:tabBarItems animated:NO];
[tabBar setSelectedItem:about];
[tabBarItems release];
[about release];
which worked fine, or at least worked as I expected.