I have created a main dialog (MainMenu.xib) and created two button on mainmenu.xib file and when click on each button, it will launch different xib file(say for button1 xib file is button1.xib and for button2 xib file is button2.xib file).
Now, My question I need the value of button1.xib file into button2.xib file.
I have tried the code
id appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate] ;
this code always gives the delegate of active dialog.
Can you please tell me how to get the control or object of inactive dialog?
Thanks,
What you need is model object. Below is really simple approach how you should start. This model object should be stored in AppDelegate for non-document based app. Second window should not know about existence of any other window. The same applies for first window. It shouldn't know more that displaying/working with model.
Accessing model through delegate in fact accessing delegate like below is considered as really, really, really bad.
id model = [(AppDelegate*)[[NSApplication sharedApplication] delegate] model];
Simple example how you can implement model and monitor for changes
#import "AppDelegate.h"
#import "FirstWC.h"
#import "SecondWC.h"
#import "Model.h"
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
#property (nonatomic, strong) FirstWC *firstWC;
#property (nonatomic, strong) SecondWC *secondWC;
#property (nonatomic, strong) Model *model;
#end
#implementation AppDelegate
- (instancetype)init
{
self = [super init];
if (self) {
_model = [[Model alloc] init];
}
return self;
}
- (IBAction)changeColor:(id)sender
{
NSUInteger number = arc4random() % 4;
switch (number) {
case 0:
self.model.color = [NSColor redColor];
break;
case 1:
self.model.color = [NSColor blueColor];
break;
case 2:
self.model.color = [NSColor purpleColor];
break;
case 3:
self.model.color = [NSColor yellowColor];
break;
default:
break;
}
}
- (IBAction)showFirstWC:(id)sender
{
if (!_firstWC) {
_firstWC = [[FirstWC alloc] initWithModel:self.model];
}
[[_firstWC window] makeKeyAndOrderFront:self];
}
- (IBAction)showSecondWC:(id)sender
{
if (!_secondWC) {
_secondWC = [[SecondWC alloc] initWithModel:self.model];
}
[[_secondWC window] makeKeyAndOrderFront:self];
}
#end
Model:
#import <Cocoa/Cocoa.h>
#interface Model : NSObject
#property (nonatomic, strong) NSColor *color;
#end
#import "Model.h"
#implementation Model
- (instancetype)init
{
self = [super init];
if (self) {
_color = [NSColor blueColor];
}
return self;
}
#end
Window Controller:
#import "FirstWC.h"
#import "Model.h"
#interface FirstWC ()
#property (nonatomic, strong) Model *model;
#end
#implementation FirstWC
- (instancetype)initWithModel:(Model *)model
{
self = [super initWithWindowNibName:#"FirstWC"];
if (self) {
_model = model;
[self addObserver:self
forKeyPath:#"model.color"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:NULL];
}
return self;
}
- (void)windowDidLoad {
[super windowDidLoad];
self.window.backgroundColor = [self model].color;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:#"model.color"]) {
self.window.backgroundColor = self.model.color;
}
}
- (void)dealloc
{
[self removeObserver:self forKeyPath:#"model.color"];
}
#end
#import "SecondWC.h"
#interface SecondWC ()
#property (nonatomic, strong) Model *model;
#end
#implementation SecondWC
- (instancetype)initWithModel:(Model *)model
{
self = [super initWithWindowNibName:#"SecondWC"];
if (self) {
_model = model;
[self addObserver:self
forKeyPath:#"model.color"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:NULL];
}
return self;
}
- (void)windowDidLoad {
[super windowDidLoad];
self.window.backgroundColor = [self model].color;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:#"model.color"]) {
self.window.backgroundColor = self.model.color;
}
}
- (void)dealloc
{
[self removeObserver:self forKeyPath:#"model.color"];
}
#end
Add properties for both button1 and button2 in your controller, and a (weak) property for button2 in the button1 controller, and vice versa.
- (void)createButtons {
DlgController1 = [[Button1 alloc] initWithWindowNibName:#"Button1"];
DlgController2 = [[Button2 alloc] initWithWindowNibName:#"Button2"];
DlgController1.button2 = DlgController2;
DlgController2.button1 = DlgController1;
}
- (IBAction)Button1:(id)sender {
[DlgController1 showWindow:self];
[NSApp runModalForWindow:DlgController1.window];
[NSApp endSheet:DlgController1.window];
[DlgController1.window orderOut:self];
[DlgController1 close];
}
I have tried the code
id appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate] ;
this code always gives the delegate of active dialog.
I'm not quite sure how this relates to the rest of your question, but the above issue suggests that you have an instance of your AppDelegate class in button1.xib and button2.xib. You should not.
There is only one application object in your app. It should have only one delegate. That instance is created in the MainMenu NIB and connected to the application's delegate outlet using the File's Owner placeholder's outlet. That should be the only instance of AppDelegate in your whole app.
If you also create an instance of AppDelegate in the other NIBs, then there will be multiple instances of that class once those NIBs are loaded, which will just confuse things. Also, if those instances are also connected to the application's delegate outlet, then they will replace the original delegate. That explains the symptom you described above where [[NSApplication sharedApplication] delegate] gives an object from the last NIB to load.
I'm not sure what button1.xib and button2.xib are supposed to contain. I'm guessing they are supposed to contain a window. In that case, you should write a custom subclass of NSWindowController to be the window controller for each window NIB. You would instantiate that class in the button's action method and provide it with whatever information it needs (possibly including a reference to the app delegate). That controller class would be responsible for loading the NIB. It would serve as the NIB's owner and would be represented in the NIB by the File's Owner placeholder. It's pretty common to make the window controller be the window's delegate, too. I recommend that you follow the advice of this blog post.
Related
Is it possible to use UIInputViewController subclass as input view on iOS 9? I've tried setting inputViewController property but no custom view controller shows up when my text field becomes a first responder.
#interface InputViewController : UIInputViewController
#end
#implementation InputViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.translatesAutoresizingMaskIntoConstraints = NO;
[self.inputView addSubview:datePicker];
[datePicker.leadingAnchor constraintEqualToAnchor:self.inputView.leadingAnchor].active = YES;
[datePicker.trailingAnchor constraintEqualToAnchor:self.inputView.trailingAnchor].active = YES;
[datePicker.topAnchor constraintEqualToAnchor:self.inputView.topAnchor].active = YES;
[datePicker.bottomAnchor constraintEqualToAnchor:self.inputView.bottomAnchor].active = YES;
}
#end
#interface TextField: UITextField
#property (nonatomic, readwrite, strong) UIInputViewController *inputViewController;
#end
#implementation TextField
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.textField.inputViewController = [[InputViewController alloc] init];
}
#end
Turned out the missing piece was:
self.view.translatesAutoresizingMaskIntoConstraints = NO;
Very frustrating that UIInputViewController does not do this automatically.
I am new to iPhone. I am trying to implement KVO mechanism.
What I have?
two TabController with two UIViewController, FirstViewController has a button,
SecondViewController has a UITextView
What I Want?
When button is pressed in the firstViewController, it updates member variable, which should be observed by secondViewController, and it should append to the UITextView.
What I did?
FirstViewController.h
#interface FirstViewController : UIViewController
{
IBOutlet UIButton *submitButton;
}
-(IBAction)submitButtonPressed;
#property (retain) NSString* logString;
#end
FirstViewController.m
-(IBAction)submitButtonPressed
{
NSLog (#" Submit Button PRessed ");
self.logString = #"... BUtton PRessed and passing this information ";
}
SecondViewController.h
#interface SecondViewController : UIViewController
{
IBOutlet UITextView *logView;
}
#property (nonatomic, strong) UITextView *logView;
#end
SecondViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
......
NSArray *vControllers = [self.tabBarController viewControllers];
FirstViewController *fvc = [vControllers objectAtIndex:0];
[fvc addObserver:self forKeyPath:#"logString" options:NSKeyValueObservingOptionNew context:NULL];
return self;
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog (#".... OBSERVE VALUE FOR KEY PATH...");
}
What output I Expect?
The String ".... OBSERVE VALUE FOR KEY PATH..." should be printed each time i press the button in the FirstViewController.
What I get?
No OUtput.
What i am doing wrong?. Kindly help me
put your "member variable" into a separate class file ... i.e. MODEL / view / controller. Make a singleton model object holding your data, then you can KVO that model object from any view controller.
This is roughed out pseudo-code:
#interface MyDataModel: NSObject
{
NSString *logString;
}
#property (nonatomic,retain) NSString *logString;
#end
#implementation MyDataModel
#synthesize logString;
+ (MyDataModel *) modelObject
{
static MyDataModel *instance = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
if (instance == nil)
{
instance = [[self alloc] init];
}
});
return (instance);
}
#end
in your VC1
MyDataModel *model = [MyDataModel modelObject];
[model setLogString:#"test"];
in your VC2
[model addObserver:self forKeyPath:#"logString" options:0 context:NULL];
a more sophisticated approach would use Core Data as a persistent store and to act as your data model.
This is my first iOS app, so I am probably missing something very simple. Please be kind. I have been tearing my hair out and I could really use some help.
Overview Of App
Basically, this is a single page application that just loads a UIWebView. I have an external accessory (bluetooth barcode scanner) that I connect and basically what I want to do is when the the app receives a scan, I want to call a method in my ViewController and update the UIWebView accordingly.
What Is Working
I am able to connect the scanner, load the first view, which loads the initial webpage, scan a barcode and call the method in my controller.
My Problem
I can't seem to figure out how to update the UIWebView from the method in my controller. It logs the url string to my debugger area, but never actually updates the webview. I am pretty sure I have some delegation wrong or something with my webview instance. There must be some glue code here that I am missing.
My Code HelloWorldViewController.h
#import <UIKit/UIKit.h>
#import "KScan.h"
#interface HelloWorldViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *page;
IBOutlet UILabel *myLabel;
Boolean IsFirstTime;
KScan *kscan;
}
- (void)setFirstTime;
- (void)DisplayConnectionStatus;
- (void)DisplayMessage:(char *)Message;
- (void)newBarcodeScanned:(NSString *)barcode;
- (void)loadBarcodePage:(NSString *)barcode;
#property (nonatomic, retain) KScan *kscan;
#property (nonatomic, retain) UIWebView *page;
#property (nonatomic, retain) UILabel *myLabel;
#end
My Code HelloWorldViewController.m
#import "HelloWorldViewController.h"
#import "common.h"
#implementation HelloWorldViewController
#synthesize myLabel;
#synthesize page;
#synthesize kscan;
- (void)setFirstTime
{
IsFirstTime = true;
}
- (void)viewDidLoad
{
self.kscan = [[KScan alloc] init];
[super viewDidLoad];
page.scrollView.bounces = NO;
//page.delegate = self;
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://192.168.0.187:3000"]]];
}
- (void) newBarcodeScanned:(NSString *)barcode
{
NSLog(#"%s[%#]",__FUNCTION__, barcode);
[self loadBarcodePage:barcode];
}
- (void)loadBarcodePage:(NSString *)barcode
{
NSLog(#"%s",__FUNCTION__);
NSString *url = [[NSString alloc] initWithFormat:#"http://www.google.com/%#", barcode];
NSLog(#"%#", url);
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
- (void)viewDidUnload
{
[myLabel release];
myLabel = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void)dealloc {
[page release];
[kscan release];
[myLabel release];
[super dealloc];
}
#end
Basically, I am just trying to load google.com into my page webview when scanning a barcode. My log statements are being logged with the correct URL, but this line of code doesn't work.
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
I am not getting any errors and my xCode debugging skills are not the greatest.
Any help would be greatly appreciated!
It looks like your webview is never allocated, or added to your main view. You are probably talking to a nil instance.
Unless your web view comes from a XIB file (which I doubt since it is not declared as an IBOutlet in your heder file) try adding something like this to your viewDidLoad:
self.page = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.page];
Im writing an App for iPad using Orientation.
The App-Delegate.h has a window, an UIViewController, an UINavigationController and an UITabbarController:
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet LoginRVC *loginRVC;
#property (nonatomic, retain) IBOutlet ChooseCameraRVC *chooseCameraRVC;
#property (nonatomic, retain) IBOutlet UITabBarController *hauptRVC;
Every Controller uses the "shouldAutorotateToInterfaceOrientation"-method to autorotate itself.
i change the views using:
[UIView beginAnimations:nil context:NULL];
and then
[loginRVC.view removeFromSuperview];
[_window addSubview:chooseCameraRVC.view];
and the other way around too, ofc.
So my problem is, when i am in the second view (chooseCameraRVC) and switch the orientation, then go back to my first view, its not rotated. It do autorotate but after the animation is completed.
I tried many things like calling "shouldAutorotateToInterfaceOrientation"-method of all views, not removing the views from window ... but no success til now.
Is this maybe a "feature" of the simulator? (i hope not).
Pls help me.
Sharky
Ok. I prepared my source code to be presented here.
Note: I didn't copy the methods which only has [super ...] within or are completely commented out.
At first the AppDelegate.h:
#import <UIKit/UIKit.h>
#import "ChooseCameraRVC.h"
#import "LoginRVC.h"
#interface NetCoWatchAppDelegate : NSObject <UIApplicationDelegate>
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet LoginRVC *loginRVC;
#property (nonatomic, retain) IBOutlet ChooseCameraRVC *chooseCameraRVC;
-(void)changeView:(id)sender:(BOOL)direction;
#end
AppDelegate.m:
#import "NetCoWatchAppDelegate.h"
#import "LoginRVC.h"
#import "ChooseCameraRVC.h"
#import "ChooseCameraVC.h"
#implementation NetCoWatchAppDelegate
#synthesize window = _window;
#synthesize loginRVC, chooseCameraRVC;
-(void)changeView:(id)sender:(BOOL)direction{
//configure animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
if(sender == loginRVC){ //sender is LoginView
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_window cache:YES];
[loginRVC.view removeFromSuperview];
[_window addSubview:chooseCameraRVC.view];
}else if(sender == chooseCameraRVC){
[chooseCameraRVC.view removeFromSuperview];
if(!direction){ //FlipFromRight = YES, ...left = NO
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_window cache:YES];
[_window addSubview:loginRVC.view];
}
}else if([sender class] == [ChooseCameraVC class]){
[chooseCameraRVC.view removeFromSuperview];
if(!direction){ //Camera gewählt //FlipFromRight = YES, ...left = NO
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_window cache:YES];
[_window addSubview:loginRVC.view];
}
}else { //default solution
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Bad Value" message:[[sender class] description] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
[av release];
}
[UIView commitAnimations]; //start animation
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_window release];
[loginRVC release];
[chooseCameraRVC release];
[super dealloc];
}
#end
The LoginRVC.h:
#import <UIKit/UIKit.h>
#interface LoginRVC : UIViewController <UITextFieldDelegate>{
NSMutableArray *usernameArray;
NSMutableArray *passwordArray;
}
#property (nonatomic, retain) IBOutlet UITextField *usernameTF;
#property (nonatomic, retain) IBOutlet UITextField *passwordTF;
#property (nonatomic, retain) IBOutlet UIButton *loginBn;
#property (nonatomic, retain) IBOutlet UISwitch *saveUsernameSwitch;
-(IBAction)tryLogin:(id)sender;
-(IBAction)closeKeyboard:(id)sender;
#end
The LoginRVC.m:
#import "LoginRVC.h"
#import "NetCoWatchAppDelegate.h"
#implementation LoginRVC
#synthesize usernameTF, passwordTF, loginBn, saveUsernameSwitch;
-(IBAction)tryLogin:(id)sender{
//login successful if the textfields are euqal with an existing account
#warning Access the data base and search for the account.
bool accountFound = NO;
for (int i=0; i<usernameArray.count; i++) {
if([[usernameArray objectAtIndex:i] isEqualToString:usernameTF.text]
&& [[passwordArray objectAtIndex:i] isEqualToString:passwordTF.text]){
accountFound = YES;
break;
}
}
if(accountFound)
{ //login successful - now change the values and then the view
if(![saveUsernameSwitch isOn])
usernameTF.text = #"";
passwordTF.text = #"";
NetCoWatchAppDelegate *main = (NetCoWatchAppDelegate*)[[UIApplication sharedApplication] delegate];
[main changeView:self:YES];
}else{ //login failt - show a popup window for the user
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Login fehlgeschlagen" message:#"Username oder Passwort falsch!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
[av release];
}
}
-(IBAction)closeKeyboard:(id)sender{
if([passwordTF isFirstResponder])
[passwordTF resignFirstResponder];
else
[usernameTF resignFirstResponder];
}
// this helps dismiss the keyboard then the "done" button is clicked
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == usernameTF){ //move to password textfield
[textField resignFirstResponder];
[passwordTF becomeFirstResponder];
}else if(textField == passwordTF){ //textField == passwordTF -> try to login
[textField resignFirstResponder];
[self tryLogin:self];
}
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
#warning Define right keyboard type.
usernameArray = [[NSMutableArray alloc] initWithObjects:#"dkoehn", #"bmazanek", #"sbehne", #"mballhausen", #"efiedler", #"bbraasch", #"azuber", #"tstolt", nil];
passwordArray = [[NSMutableArray alloc] initWithObjects:#"test1",#"test2",#"test3",#"test4",#"test5",#"test6",#"test7",#"test8", nil];
// usernameTF.keyboardType = UIKeyboardTypeEmailAddress;
[usernameTF becomeFirstResponder]; //get first focus when the app stars
//set return key on the keyboard and the delegate for an action
usernameTF.returnKeyType = UIReturnKeyNext; // type of the return key
passwordTF.returnKeyType = UIReturnKeyGo;
//set delegate to connect with a method "-(BOOL)textFieldShouldReturn:(UITextField *)textField"
usernameTF.delegate = self;
passwordTF.delegate = self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#end
The ChooseCameraRVC.h:
#import <UIKit/UIKit.h>
#interface ChooseCameraRVC : UINavigationController <UINavigationControllerDelegate>
#property (nonatomic, retain) IBOutlet UIBarButtonItem *zurueckBN;
-(IBAction)exitToLoginView:(id)sender;
#end
The ChooseCameraRVC.m:
#import "ChooseCameraRVC.h"
#import "NetCoWatchAppDelegate.h"
#import "ChooseCameraCell.h"
#implementation ChooseCameraRVC
#synthesize zurueckBN;
-(IBAction)exitToLoginView:(id)sender{
#warning Eventually logout the User.
//change the view
[((NetCoWatchAppDelegate*)[[UIApplication sharedApplication] delegate]) changeView:self:NO];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#end
ChooseCameraVC.h:
#import <UIKit/UIKit.h>
#interface ChooseCameraVC : UITableViewController <UITableViewDelegate>
#end
and the ChooseCameraVC.m:
#import "ChooseCameraVC.h"
#import "ChooseCameraCell.h"
#import "NetCoWatchAppDelegate.h"
#implementation ChooseCameraVC
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Customize the number of sections if grouped.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
#warning Get count of cameras out of the data base.
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = #"Camera";
return cell;
}
#end
I hope u can find the problem.
Greetings. $h#rky
now i found my mistake. as u can see i have the views as variables in the app delegate. so if the second view changes the orientation, the other ones didn't know a thing about it. if the view now changes the "new" one recognizes the orientation change AFTER the animation, so while the animation is running, the "new" view has the wrong orientation.
So if u want to switch a view, just create a new one because it gets initialized with the right orientation.
kind regards
$h#rky
For support all orientations your viewcontroller should implement shouldAutorotateToInterfaceOrientation like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
Every viewcontroller should implement this method for support required orientations.
Check also Supported interface orientations item in .plist file. Maybe you have wrong parameters.
Try this in your secondviewcontroller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
UIViewController *controller = [self.navigationController.viewControllers objectAtIndex:0];
[controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
return YES;
}
Hope it works..!!:)
Bah. I've pulled my hair out over this problem for the past couple days now, but I know I must be overlooking the obvious. I've made my PickerViewController(.h./m) and PickerViewAppDelegate(.h/.m) files and they run fine as a standalone program, but I would like to have the picker pop up after a procedureal event occurs in my "helloworld.m" file. I can get the picker to show up, but I cannot for the life of me figure out how to populate it so that it isn't blank. I THINK I've done everything right up until I try to pass my array to my pickerview object. What am I doing wrong?
PickerViewController.h
#import <UIKit/UIKit.h>
#interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *scrollerData;
}
#property (nonatomic, retain) IBOutlet UIPickerView *pickerView;
#property (nonatomic, retain) NSMutableArray *scrollerData;
-(void)setScrollerData:(NSMutableArray *)array;
#end
PickerViewController.m
#import "PickerViewController.h"
#implementation PickerViewController
#synthesize pickerView, scrollerData;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
// [arrayColors release];
[super dealloc];
}
-(void)setScrollerData:(NSMutableArray *)array
{
//[self.scrollerData arrayByAddingObjectsFromArray:array];
scrollerData = array;
}
#pragma mark -
#pragma mark Picker View Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [scrollerData count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [scrollerData objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Number: %#. Index of selected numbers: %i", [scrollerData objectAtIndex:row], row);
}
PickerViewAppDelegate.h
#import <UIKit/UIKit.h>
#class PickerViewController;
#interface PickerViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PickerViewController *pvController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
PickerViewAppDelegate.m
#import "PickerViewAppDelegate.h"
#import "PickerViewController.h"
#implementation PickerViewAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
pvController = [[PickerViewController alloc] initWithNibName:#"PickerView" bundle:[NSBundle mainBundle]];
[window addSubview:pvController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[pvController release];
[window release];
[super dealloc];
}
#end
Helloworld.m
...
UIView* view = [[CCDirector sharedDirector] openGLView];
UIPickerView *pickerView=[[UIPickerView alloc] init];
pickerView.frame=CGRectMake(100,100, 200, 200);
NSMutableArray *arrayNumbers = [[NSMutableArray alloc] init];
[arrayNumbers addObject:#"30"];
[arrayNumbers addObject:#"31"];
[arrayNumbers addObject:#"32"];
[arrayNumbers addObject:#"33"];
[arrayNumbers addObject:#"34"];
[arrayNumbers addObject:#"35"];
[arrayNumbers addObject:#"36"];
[pickerView setscrollerData: arrayNumbers];//Should I be calling pickerView here or something else?
[view addSubview: pickerView];
pickerView.hidden=NO;
...
You have overridden the setter method generated by #synthesize in PickerViewController, so you are no longer retaining it.
Then, you are calling setScrollerData on your pickerView (this should be giving you a warning or crashing since pickerView doesn't respond to that method).
You are not setting PickerViewController as the delegate or datasource of your picker view in helloworld.m.
I can't see where your hello world code fits in. It seems to be adding a new picker view rather than using the one from the xib of PickerViewController. You should be instantiating pickerviewcontroller from your hello world and adding its .view as a subview or presenting it as a modal view controller rather than setting up a new picker view. You can then pass your array to the instance of pickerviewcontroller. Note though that it is not standard to have a separate view controller for what is essentially a subview, though I don't have much knowledge of cocos2d so I don't know if this is normal when using that framework.
Well, i think you should just pass the array from HelloWorld class to PickerViewController class using property/synthesize.