control an object from different .h on main window - iphone

my application is window based application in objective-c
I have appdelegate file (.h and .m) and controller (.h and .m)
the program load the window view from the appdelegate file and shows the objects (slider, labels) which defined in controller so:
didFinishLaunchingWithOptions in appdelegate
the label in controller
all i need to know how can i get control on the label from that function.

If you establish the label as a property with getter and setter methods:
in controller.h:
{
IBOutlet UILabel *label;
}
#property(nonatomic, retain) IBOutlet UILabel *label;
in controller.m:
#synthesize label;
Then you can simply call the label from an instantiation of controller:
[controller.label setText:#"For example, you can set the text"];

Related

How to add Connection Outlets from separate class file (.m) Objects to ViewController's MainStoryboard

I want to maintain data encapsulation and have separated an NSObject class (.h and .m file) from my ViewController.m.
I have the Objective-C working correctly where my class is instantiated in the ViewController's viewDidLoad and I can set, get and NSLog the private values via my NSObject's methods.
What I can't do is in the MainStoryboard assign the Connection Outlets and Received Actions. My IBOutlets (a UILabel and UIButton) aren't showing in the Connection Inspector. However, I have many Objects in my ViewController's .[hm] file that I can setup the Outlet Connections to. It's just this new file's Objects that I can't view in the storyboard tool.
What am I doing wrong?
// GameTimer.h
#import <Foundation/Foundation.h>
#interface GameTimer : NSObject {
UILabel *gameTimerLabel;
NSTimer *gameTimer;
unsigned int gameTimerTicks;
}
#property unsigned int gameTimerTicks;
#property (nonatomic, retain) IBOutlet UILabel *gameTimerLabel;
#property (nonatomic, retain) IBOutlet UIButton *startButton;
// instantiate the timer
- (IBAction)onStartPressed:(id)sender;
// Update the gameTimerLabel, show new value to user
- (void)gameTimerShow;
// selector func for our timer, manages the tick count for all our timers
- (void)gameTimerEvent;
#end
// FirstViewController.m
#import "FirstViewController.h"
#import "GameTimer.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
GameTimer *myGameClock;
- (void)viewDidLoad
{
[super viewDidLoad];
myGameClock = [[GameTimer alloc] init];
[myGameClock setGameTimerTicks:33*10];
[myGameClock gameTimerShow];
unsigned long myticks = myGameClock.gameTimerTicks;
NSLog(#"Ticks=%lu", myticks);
}
I think you may be confusing encapsulation the role of a controller. Since the Cocoa Touch framework is a Model-View-Controller, the Controller is the "manager" that sits between the View's user interpretation and the Model's data and business rules. Therefore you must put your IBOutlets and IBActions in your UIViewController subclasses.
Build your timer into a separate class. The timer would then be considered part of the model that other controllers or other objects of the model can instantiate as needed. Let your Controller instantiate a "Timer". Then use the Controller to manage the "Timer" operations. If you need to display the elapsed time, then the Controller should get the elapsed time from the "Timer" object and put it in the appropriate control. If you need to set the length of time in the "Timer" then the Controller will get the value from a View's control and put it in the "Timer".
Hope this helps

Can't pass UITableViewCell didSelectRowAtIndexPath data to another view controller

I have currently got a UITableView in my main view controller. I want the data of the selected row to be passed to another view controller in my project.
Here's what I have got so far, although there is an error. Why is this? I have referenced the class and the header file in my .h file.
I would really appreciate some help with this as I've tried everything I can think of.
Simply synthesize the object in the destination view.
It will work fine...
like:
in .h
#property(nonatomic, retain) UILabel *note;
in .m
#synthesize note;
Very Simple,I think note is a UILabel,
according to You error
please use this code in AddNote. h file
NSString *noteStr; //Ios 4
IBoutlet UILabel *note;
#property(nonatomic, retain) NSString *noteStr; //Ios 4
#property(nonatomic, retain) UILabel *note;
#property(nonatomic, strong) NSString *noteStr;
#property(nonatomic, strong) UILabel *note; //Ios 5
In AddNote. .m file
#synthesize note,noteStr;
self.note.text=noteStr; //in ViewWillAppear
Some Time Application is crash Because Memory IF you Work In Ios 4 Please Correct this code
an.noteStr - [selectNote retain];
Declare static variable for global use.
Using keyword extern
Create Common.h
extern NSMutableArray *gMyBasketCollectionList;
common.m
NSMutableArray *gMyBasketCollectionList;
Use This Mutable Aarray to Store Your Data and Display in Any other
include common.h in First View And SecondView
Add Object to mutable Array in First View And
Show SecondView And Display access that Array

Do I need to create variables and link IBOutlet for every UIVIew?

I have a View Controller that is swapping UIView objects in and out. There is the potential to have hundreds of different views, each with their own behaviors.
Within my current MainWindow.xib file I currently have:
File's Owner UIApplication
First Responder UIResponder
AppDelegate AppDelegate
-Cover Cover
Window UIWindow
Table of Contents TableOfContents
page1 Page1
page2 Page2
page...n Page...n
The AppDelegate declares the window and the viewController. It's pretty basic.
MainViewController.h
#import <UIKit/UIKit.h>
#class TableOfContents, Page1;
#interface MainViewController : UIViewController {
TableOfContents *tableOfContents;
Page1 *page1;
Page2 *page2;
Page...n *page...n;
}
#property (nonatomic, retain) IBOutlet TableOfContents *tableOfContents;
#property (nonatomic, retain) IBOutlet Page1 *page1;
#property (nonatomic, retain) IBOutlet Page2 *page2;
#property (nonatomic, retain) IBOutlet Page...n *page...n;
-(IBAction)funcGoToPage:(id)sender;
#end
MainViewController.m
#import "MainViewController.h"
#import "TableOfContents.h"
#import "Cover.h"
#import "Page1.h"
#import "Page2.h"
#import "Page...n.h"
#implementation MainViewController
#synthesize page1, page2, page...n tableOfContents;
#synthesize pageID, pagesPathFile, pagesPath;
-(IBAction)funcGoToPage:(id)sender{
//[[self view] removeFromSuperview];
[self.view addSubview:self.tableOfContents];
}
The corresponding UIView classes are pretty bare at the moment so I'll refrain from posting them.
Right now funcGoToPage is just bringing up tableOfContents. Eventually I'll have it go different places depending on what was clicked.
Currently each page is set up as an IBOutlet and linked from the MainViewController to the appropriate UIView in Interface Builder. Done this way each page will have to be set up as a variable and linked to in IB creating a hubub of variables, outlets and connections.
My question is: Is there a way to create these connections on the fly so that I can swap them in using my funcGoToPage function without setting them up as an IBOutlet?
When a nib is loaded all of its content is loaded. If you have lots of views in one nib you'll quickly run out of memory.
I would put each page in a seperate nib and then load the nib when required:
[[NSBundle mainBundle] loadNibNamed:#"nibNameWithoutExtension" owner:self options:nil];
For this to work:
add an IBOutlet, eg newPage, to whatever self refers to
set the File Owner in nibNameWithoutExtension to whatever self refers to
join the view in nibNameWithoutExtension to the newPage outlet of File Owner

Navigation Within TabController

I am trying to use UITabController as may controller in my main window and add navigation controllers to some tab bar items.
For example, the first tab has a navigation controller with table view:
alt text http://www.freeimagehosting.net/uploads/f3ad987c86.png
The SettingsViewController is associated with its own NIB file, where a table view is defined. Within that xib file, I have a table view and set it to the outlet of SettingsViewController class property myTableView.
Here are my h files:
// header file for SettingViewController class
#interface SettingsViewController :
UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *myTableView;
// other codes vars
}
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
// ...
#end
// header for main app delegate
#interface MainAppDelegate :
NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
// ...
}
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
// ...
#end
In my SettingsViewController.xib file, through IB, I linked outlet myTableView to the xib's file owner, ie, SettingViewController class:
alt text http://www.freeimagehosting.net/uploads/e577d35137.png
The problem is that in the main xib file, for the SettingsViewController navigation, there is one outlet myTableView. I am not sure if I have to set this to somewhere?
The exception I get is "[UIViewController _loadViewFromNibNamed:bundle:] loaded the "SettingsViewController" nib but the view outlet was not set."
SettingsViewController already has a view property. Are you sure this one is hooked up in Interface Builder? (You probably want it to be hooked up to your UITableView.)

Should I put my UITabBarController outside the App Delegate?

I followed an example from "Beginning iPhone 3 Development" which puts the code for the main view controller, a Tab Bar, in the delegate method. Is this the correct place to put this or should it be in a separate .h and .m file? All my subviews are in separate files so I'm wondering if I should have my tab bar view controller code in a separate file also.
Also, for the subviews I call ViewDidLoad as normal but there is no ViewDidLoad in the delegate method, I guess because it's of type NSObject and not UIViewController. Should I change the delegate to a type UIViewController so I can call ViewDidLoad?
Thanks, code samples of my existing app are below.
Header file for Delegate:
#import <UIKit/UIKit.h>
#interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *rootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#end
Beginning of Delegate implementation file
#import "MyAppDelegate.h"
#implementation MyAppDelegate
#synthesize window;
#synthesize rootController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Add the tab bar controller's current view as a subview of the window
[window addSubview:rootController.view];
[window makeKeyAndVisible];
}
Is this the correct place to put this or should it be in a separate .h and .m file?
Should I change the delegate to a type UIViewController so I can call ViewDidLoad?
no this is your initial load point, not a view controller. Even if you change its type, the view did load method will not be called, the app delegate is not a view controller. It is here you load your initial view controller. UITabbar (according to the doco) "This class is not intended for subclassing." see here. (so no .h and .m file, what would you derive from?) you should not need to subclass, as you will get your viewdidload method for each of the views you put in your tab bar.