How to load multiple xibs in a single xib? - iphone

I've created 4 xibs of width 200,height 200 for a purpose...but if i want to load all xib in a single xib i dont know to achieve that..I got a couple of ideas
1.Can we make all the xib to load by group of arrays?
2.Can we declare NSBUNDLE code at appdelegate.m file?..

each xib must have a class for control it. So you now must have 4 xib , 4 .h and 4 .m files. Now create another view controller with xib, .m and .h (MainViewController).
Now import all the class.
in MainController.h
#import <UIKit/UIKit.h>
#import "Xib1Class.h"
#import "Xib2Class.h"
#import "Xib3Class.h"
#import "Xib4Class.h"
#interface MainViewController : UIViewController {
Xib1Class *xib1Class;
Xib2Class *xib2Class;
Xib3Class *xib3Class;
Xib4Class *xib4Class;
}
#end
in viewDidLoad of MainViewController.m
xib1Class = [[Xib1Class alloc] initWithNibName:#"Xib1Class" bundle:nil];
xib2Class = [[Xib2Class alloc] initWithNibName:#"Xib2Class" bundle:nil];
xib3Class = [[Xib3Class alloc] initWithNibName:#"Xib3Class" bundle:nil];
xib4Class = [[Xib4Class alloc] initWithNibName:#"Xib4Class" bundle:nil];
//set the frame
xib1Class.view.frame = CGRectMake(x,y,w,h);
..........
//put in main view
[self.view addSubView:xib1class.view];
//the same for 2-3-4
remember to manage memory.
Hope this help u.

Related

How to add multiple tables in UITableViewController

Any one please tell me how to add two tables in uiview controller?
I created a class object in rootviewController as
mainDataViewController=[[MainDataViewController alloc]initWithStyle:UITableViewStylePlain];
And in mainDataviewController taken as UITableviewController
#interface MainDataViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
Now, I Want to add three tables in mainDataViewController.
Please give me some idea for solving this.
follow these steps.
1)Create a tableViewController with xib.
2)Create only two other xib's as given screenshots below:--
3)Drag uitableView from object window.
4)Change its class to TableViewController's class you have firstly created.
5)Connect file owner's view delegate to tableView.
6) And use code as ------
- (void)viewDidLoad
{
MYViewController *FirstTableController=[[MYViewController alloc] initWithNibName:#"MYViewController" bundle:nil];
MYViewController *secondTableController=[[MYViewController alloc] initWithNibName:#"MYSecondController" bundle:nil];
MYViewController *thirdTableController=[[MYViewController alloc] initWithNibName:#"MYThird" bundle:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Or
#import <UIKit/UIKit.h>
#interface MYViewController : UITableViewController
{
IBOutlet UITableView *f_table,*s_table,*t_table; //outlets for different tableViews in xib.
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.view=f_table; // when working with first table.
self.view=s_table; //working with second table.
self.view=t_table; //working with third table.
}
I think you have not searched well. Please have a look at this.
How to use 2 UITableView in a UIViewController?
1) First add tableview on your viewController xib file
2) declare variables for your tableviews
3) synthesize properthies
4) add outlets to your propethies
Now you can use them...

How Can I Access my ViewController's Property Variables From Another Class?

Ok, this is really bugging me and I am sure the solution is simple... I am unable to set my ViewController's property variables from another class (SeverConnect.m), which I have declared and synthesized properly in my ViewController's .h/.m files:
ServerConnect.h:
#import <Foundation/Foundation.h>
#import "ViewController.h"
#import "Contact.h"
#class ViewController;
#interface ServerConnect : NSObject
{
Contact *newContact;
NSString *codeRawContent;
NSMutableArray *contactListCopy;
... //Other variables declared here, but not shown in order to save space
Inside ServerConnect.m:
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(#"parserDidFinish");
newContact = [[Contact alloc] initWithCodeInfo:(NSString *)codeInfo
contactName:(NSString *)completeName
contactImage:(UIImage *)profileImage
contactWebSite:(NSString *)codeRawContent];
[contactListCopy insertObject:newContact atIndex:0];
[ViewController setContactList:contactListCopy]; //Automatic Reference Counting Error Occurs Here: "No known class method for selector 'setContactList:'"
}
As I mentioned above, I have declared and synthesized the property variable, "contactList", in my ViewController's .h/.m files (with no errors):
#property (nonatomic, retain) NSMutableArray *contactList; //In ViewController's .h file
#synthesize contactList; //In ViewController's .m file
Can someone tell me what I am doing wrong? Thanks in advance for any help you can provide!
you are trying to access an instance property on a class:
[ViewController setContactList:contactListCopy];
you need to first create an instance of the ViewController class, and then set its property. Something like this:
ViewController *viewController = [[ViewController alloc] init];
[viewController setContactList:contactListCopy];
In this line of code:
[ViewController setContactList:contactListCopy];
you should be using a variable of type ViewController. The way you are using it, it should be a class method, not a property.
Write something like:
ViewController *viewController = [[ViewController alloc] init];
[viewController setContactList:contactListCopy];

When using StoryBoards, am I allowed to present view controllers using presentViewController:animated:completion:?

I would like to know if, when Im using storyboards, can I still present view controllers using the method presentViewController:(UIViewController *) animated:(BOOL) completion:^(void)completion ?
Or do I have to use segues?
In my project there is a VC that could be presented by any of the other VCs from the hole app, so if I would use segues, there would be like 20 segues to this same VC.
Thank you.
That should still work. You would use instantiateViewControllerWithIdentifier: to create the view controller from its storyboard definition before calling that method.
You can basically assign a StoryBoard ID to the UIViewController you want to present by any other UIViewController.
Then you have to Import the UIViewController subclass in the top of the .h file of the UIViewController that you want it to present it, for Example I've here the BaseViewController and the InfoViewController that I want to present:
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface BaseViewController : UIViewController
{
InfoViewController *InfoViewController;
}
#property (nonatomic, strong) InfoViewController *InfoViewController;
Then in the .m file you have to synthesize it and type implement the code. I'm using here a button to show the InfoViewController with an IBAction named ShowInfoAction.
#synthesize InfoViewController = _InfoViewController;
- (IBAction)ShowInfoAction:(id)sender {
InfoViewController *InfoVC = [self.storyboard instantiateViewControllerWithIdentifier:#"GiveItAnIDHere"];
[self presentViewController:InfoVC animated:YES completion:NULL];
}

XCode: Call action in main view from modal view

I am trying to call an action (changeMainNumber) in a main view controller from a modal view controller. The action should change the UILabel mainNumber to 2. In ViewController.h, I have:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UILabel *mainNumber;
}
#property (nonatomic, retain) UILabel *mainNumber;
-(IBAction)changeMainNumber;
ViewController.m:
#import "ViewController.h"
#implementation ViewController
#synthesize mainNumber;
- (IBAction)changeMainNumber:(id)sender {
mainNumber.text = #"2";
}
The next view controller is the modal view controller.
ModalViewController.h:
#import <UIKit/UIKit.h>
#class ViewController;
#interface ModalViewController : UIViewController {
}
-(IBAction)callChangeMainNumber:(id)sender;
and ModalViewController.m:
#import "ModalViewController.h"
#implementation ModalViewController
- (IBAction)callChangeMainNumber {
ViewController *viewController = [[ViewController alloc] init];
[viewController changeMainNumber];
}
With this setup the app keeps crashing when callChangeMainNumber is called and I can't figure out what is wrong. Any help you can provide is appreciated!
The code you posted from your ModalViewController is not referencing your ViewController. You are creating a new one in your code. The best solution to your problem would be to make your ViewController a delegate to the ModalViewController.
So in your ModalViewController.h file you should have this code above your #implementation.
#protocol ModalViewControllerDelegate
- (void)shouldChangeMainNumber;
#end
Then in your #implementation of the header have:
#property (nonatomic,assign)IBOutlet id <ModalViewControllerDelegate> delegate;
Now in the .m file where you have your IBAction method, tell the delegate that you want it to change the main number.
- (IBAction)callChangeMainNumber {
[self.delegate shouldChangeMainNumber];
}
Then in your ViewController.m file you need to set yourself as the delegate of the ModalViewController, usually in viewDidLoad is a good place to put it. So create a property in your header for the ModalViewController first and synthesize it, then add this to viewDidLoad.
self.modalViewController.delegate = self;
and finally you need to implement the delegate method in your .m file somewhere
- (void)shouldChangeMainNumber {
mainNumber.text = #"2";
}

Obj-C Sending Messages Between Classes

I'm a newbie in iPhone Programming. I'm trying to send a message from one view controller to another. The idea is that viewControllerA takes information from the user and sends it to viewControllerB. viewControllerB is then supposed to display the information in a label.
viewControllerA.h
#import <UIKit/UIKit.h>
#interface viewControllerA : UIViewController
{
int num;
}
-(IBAction)do;
#end
viewControllerA.m
#import "viewControllerA.h"
#import "viewControllerB.h"
#implementation viewControllerA
- (IBAction)do {
//initializing int for example
num = 2;
viewControllerB *viewB = [[viewControllerB alloc] init];
[viewB display:num];
[viewB release];
//viewA is presented as a ModalViewController, so it dismisses itself to return to the
//original view, i know it is not efficient
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[super dealloc];
}
#end
viewControllerB.h
#import <UIKit/UIKit.h>
#interface viewControllerB : UIViewController
{
IBOutlet UILabel *label;
}
- (IBAction)openA;
- (void)display:(NSInteger)myNum;
#end
viewControllerB.m
#import "viewControllerB.h"
#import "viewControllerA.h"
#implementation viewControllerB
- (IBAction)openA {
//presents viewControllerA when a button is pressed
viewControllerA *viewA = [[viewControllerA alloc] init];
[self presentModalViewController:viewA animated:YES];
}
- (void)display:(NSInteger)myNum {
NSLog(#"YES");
[label setText:[NSString stringWithFormat:#"%d", myNum]];
}
#end
YES is logged successfully, but the label's text does not change. I have made sure that
all of my connections in Interface Builder are correct, in fact there are other (IBAction)
methods in my program that change the text of this very label, and all of those other methods work perfectly...
Any ideas, guys? You don't need to give me a full solution, any bits of information will help. Thanks.
With
viewControllerB *viewB = [[viewControllerB alloc] init];
you are instantiating a new viewControllerB. This is not the viewControllerB that (I presume) you have in your nib file. You should make an outlet for that and wire it up.
Otherwise, possibly instantiate it with [... initWithNibName:] from a nib, instead of just [... init], and then (either way) push the instantiated view controller using [self.navigationController pushViewController:viewB animated:YES], or by presenting it modally as you seem to have mastered already.
As a sidenote, maybe name the viewcontroller variable viewConB, since there is a clear and important distinction between views and view controllers. Furthermore, class names tend to start with upper case, and variables with lower case, to keep things clear.