Multiple Views for UITabBarController - view

I'm trying to build an app where I have a TabBarController with 4 entries.
When I select the first entry, a view with a UITableView shows up.
This TableView is filled with several entries.
What I would like to do is:
When an entry out of that UITableView gets selected, another view should show up; a detailview.
.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
if(self.secondView == nil) {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:[NSBundle mainBundle]];
self.secondView = secondViewController;
[secondViewController release];
}
// Setup the animation
[self.navigationController pushViewController:self.secondView animated:YES];
}
}
.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#interface FirstViewController : UIViewController {
SecondViewController *secondView;
NSMutableArray *myData;
}
#property (nonatomic, retain) SecondViewController *secondView;
#property (nonatomic, copy, readwrite) NSMutableArray* myData;
#end
This is what I have so far.
Unfortunately.. the code runs, bit the second view does not show up.

Is your first view controller wrapped in a UINavigationController? When you set up your UITabBarController, you should add UINavigationControllers rather than your UIViewController subclasses, e.g.:
FirstViewController *viewControl1 = [[FirstViewController alloc] init];
UINavigationController *navControl1 = [[UINavigationController alloc] initWithRootViewController:viewControl1];
UITabBarController *tabControl = [[UITabBarController alloc] init];
tabControl.viewControllers = [NSArray arrayWithObjects:navControl1, <etc>, nil];
//release everything except tabControl
Also, based on your code, you don't need to keep your secondViewController as an ivar, since UINavigationController automatically retains its view controllers (and retaining it when you're not displaying it will use up unnecessary memory).

Related

could not dequeue collection view when using UIImage objects in Cell

I am having trouble getting this code to run after having it crash after several iterations. The intended behaviour is to simply display images in a Collection View from an array. If I comment out all the objects in the array, it runs with an empty collection view.
logo.png exists and can be properly loaded in other parts of the app through the dropdown. Delegate and DataSource is properly set to self. The Collection View has a single cell displayed with an ImageView in it (tagged as 100) and nothing else. Having a label instead of an image view causes a crash with any objects in the array as well.
The debugging output is
Could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must
register a nib or a class for the identifier or connect a prototype cell in a storyboard
The relevant code is the following:
ViewController.m
#import "demo_frameworkViewController.h"
#interface demo_frameworkViewController ()
#end
#implementation demo_frameworkViewController {
NSMutableArray *imageArray;
}
#synthesize imageArray;
- (void)viewDidLoad
{
imageArray = [[NSMutableArray alloc] init];
[imageArray addObject:[UIImage imageNamed:#"logo.png"]];
[imageArray addObject:[UIImage imageNamed:#"logo.png"]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark Collection View Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.imageArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIImageView *imageDemo = (UIImageView *)[cell viewWithTag:100];
imageDemo.image =[imageArray objectAtIndex:indexPath.row];
[cell.layer setBorderWidth:2.0f];
[cell.layer setBorderColor:[UIColor whiteColor].CGColor];
return cell;
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#interface demo_frameworkViewController : UIViewController <UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
#property (nonatomic, retain) NSArray *imageArray;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
The debugging output means that it cannot find a cell called 'Cell'. In your storyboard you need to set the Collection Reusable View value of the UICollectionViewCell to 'Cell' so that the UICollectionView knows what cell to use

blank screening after going back

I've recently created a app with Xcode. When 2 images collide, it sends me to a new screen (which is good). However, when i push the button to return me back to the original view, it goes blank. Can you tell me why? Any feed back is appreciated.
Here is code from the First .h file:
import
#interface ViewController : UIViewController {
IBOutlet UIImageView *image1;
IBOutlet UIImageView *image2;
}
#property (nonatomic, retain) UIImageView *image1;
#property (nonatomic, retain) UIImageView *image2;
-(void)collision;
#end
And from the .m:
import "ViewController.h"
import "newview.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize image1, image2;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *mytouch = [[event allTouches] anyObject];
image1.center = [mytouch locationInView:self.view];
[self collision];
}
-(void)collision {
if (CGRectIntersectsRect(image1.frame, image2.frame)) {
newview *second = [[newview alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
}
here is code from the second .h (The one with the go back button):
(IBAction)retry:(id)sender;
and from the .m:
import "newview.h"
import "ViewController.h"
#interface newview ()
#end
#implementation newview
(IBAction)retry:(id)sender {
ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
The blank screen is another modal view from (IBAction)retry:(id)sender.
You should use [self dismissModalViewControllerAnimated:YES]; in (IBAction)retry:(id)sender for going back to previous screen instead of presenting another modal view.

Load UIView defined in XIB into UIView

following a post presented here, I tried to load a view defined in a XIB file into a view, currently being displayed. Basically, what I want to do is to replace the middle view (see screenshot below) with a different view when the user clicks on the button.
This is the simple view I want to load into the middle view:
And this is the source code of the ViewController:
ViewController.h
#import
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *loadxibButton;
#property (strong, nonatomic) IBOutlet UIView *middleView;
- (IBAction)buttonClicked:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)buttonClicked:(id)sender {
NSLog(#"click...");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
self.middleView= nibView;
}
#end
When I click on the button, nothing happens, i.e. the new view will not be displayed. Can anybody please help?
Thanks a lot
Christian
Phillip, thanks a lot!
Instead of
- (IBAction)buttonClicked:(id)sender {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
self.middleView= nibView;
}
I now use this code:
- (IBAction)buttonClicked:(id)sender {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
nibView.frame = CGRectMake(261, 0, 532, 748);
[[[self view] viewWithTag:2] removeFromSuperview];
[[self view] addSubview:nibView];
}
Is this want you meant, i.e. is this like "best practice"?
Bye
Christian
Your view controller has a view property which, presumably, has three subviews (left, middle, and right). Instead of just setting a middleView property in your controller, your strategy should be to update the primary view's subviews array (removing the one that's to be replaced and inserting the new one).

Modify uilabel from a view to other - iphone sdk

I have TableViewController & DetailViewController.
I'd like to modify one label named 'label' in DetailViewController.
TableViewController.h :
#interface CodexTableView : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *listOfItems;
DetailViewController *vc;
}
#property (nonatomic, retain) DetailViewController *vc;
#property (strong, nonatomic) IBOutlet UITableView *myTable;
#end
And an extract of my TableViewController.m :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
vc.label.text = #"bonjour";
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//NSLog(#"%#",cell.textLabel.text);
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
dvController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
But it doesn't works.
Someone know the error ?
Please help me.
Thanks :)
You mix up two variables for your detail view controller. Remove the dvController one and only work with your vc property, like so:
vc = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[[vc label] setText:#"bonjour"];
Make label a property of your detail view controller and have it point at your onscreen label.

Why won't this simple view switching project work?

I'm in the process of learning how to write apps in Xcode. I'm using a book to guide me. Unfortunately the book is written with guides to Xcode 3 and I'm using Xcode 4.
Now so far there haven't been any problems, but this project doesn't work, and I simply don't get it, because it seems to make pretty good sense.
The project's goal is to use a view controller to switch between three views.
Could anyone please take a look and see what I did wrong?
Here is the entire project: http://www.2shared.com/file/CKO6ACzg/MultipleViews.html
PS: I know that as it is now the views will be stacked on top off each other and that the view isn't being cleared when you click a new button.
MultipleViewsViewController.h should be:
#import <UIKit/UIKit.h>
#class FirstViewController;
#class SecondViewController;
#class ThirdViewController;
#interface MultipleViewsViewController : UIViewController {
IBOutlet FirstViewController *firstViewController;
IBOutlet SecondViewController *secondViewController;
IBOutlet ThirdViewController *thirdViewController;
}
//#property (nonatomic, retain) FirstViewController *firstViewController;
//#property (nonatomic, retain) SecondViewController *secondViewController;
//#property (nonatomic, retain) ThirdViewController *thirdViewController;
-(IBAction)loadFirstView:(id)sender;
-(IBAction)loadSecondView:(id)sender;
-(IBAction)loadThirdView:(id)sender;
#end
MultipleViewsViewController.m should be:
#import "MultipleViewsViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#implementation MultipleViewsViewController
//#synthesize firstViewController;
//#synthesize secondViewController;
//#synthesize thirdViewController;
-(IBAction)loadFirstView:(id)sender{
[secondViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:firstViewController.view atIndex:0];
}
-(IBAction)loadSecondView:(id)sender{
[firstViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:secondViewController.view atIndex:0];
}
-(IBAction)loadThirdView:(id)sender{
[firstViewController.view removeFromSuperview];
[secondViewController.view removeFromSuperview];
[self.view insertSubview:thirdViewController.view atIndex:0];
}
-(void)dealloc{
[firstViewController release];
[secondViewController release];
[thirdViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
firstViewController = [[FirstViewController alloc] init];
secondViewController = [[SecondViewController alloc] init];
thirdViewController = [[ThirdViewController alloc] init];
[self loadFirstView:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Connect your buttons (which you haven't done in your project, which also might be the issue) and you're done.

Resources