I'm finding it difficult to get started in Objective-C.
I'm executing the following code when a button is clicked:
NSLog(#"hi");
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:mainMenuDriver animated:YES];
I can see "hi" in the console when I hit the button, it's just that the view should change to MainMenuDriver. But nothing happens!
Please help!
As per request for more code:
MainMenuDriver.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MainMenuDriver : UIViewController <UINavigationControllerDelegate,CLLocationManagerDelegate>{
IBOutlet UIButton *photos;
IBOutlet UIButton *profile;
IBOutlet UISwitch *onOffline;
IBOutlet UILabel *label1;
NSTimer *uploadGPS_timer;
CLLocationManager *lm;
NSString *lat;
NSString *lng;
}
#property(nonatomic,retain) UIButton *photos;
#property(nonatomic,retain) UIButton *profile;
#property(nonatomic,retain) UISwitch *onOffline;
#property(nonatomic,retain) UILabel *label1;
#property (nonatomic,retain) NSTimer *uploadGPS_timer;
#property(nonatomic,retain) NSString *lat,*lng;
-(IBAction)showMessages:(id)sender;
-(IBAction)showFriends:(id)sender;
-(IBAction)showPhotos:(id)sender;
-(IBAction)showProfile:(id)sender;
-(IBAction)switchSwitched:(id)sender;
-(void)uploadGPS_tick:(NSTimer*)timer;
#end
MainMenuDriver.m
#import "MainMenuDriver.h"
#import "ASIFormDataRequest.h"
#import "JoeMaxiViewController.h"
#import "Photos.h"
#import "Profile.h"
#implementation MainMenuDriver
#synthesize messages,profile,photos,friends,label1;
#synthesize onOffline;
#synthesize uploadGPS_timer;
#synthesize lat,lng;
-(IBAction)showPhotos:(id)sender{
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil]];
Photos *x= [[Photos alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:x animated:YES];
}
-(IBAction)showProfile:(id)sender{
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil]];
Profile *x= [[Profile alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:x animated:YES];
}
-(void)logout:(id)sender{
if([uploadGPS_timer isValid]){
[uploadGPS_timer invalidate];
}
[lm release];
//[uploadGPS_timer release];
[self.navigationController popViewControllerAnimated:NO];
/*NSString *urlStr=[[NSString alloc] initWithFormat:#"http://www.prestocab.com/driver/ajax/logout.php"];
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *response=[request responseString];
NSLog(#"%#",response);
}];
[request setFailedBlock:^{
}];
[request startAsynchronous];*/
}
-(IBAction)switchSwitched:(id)sender{
if(onOffline.on){
[label1 setText:#"For Hire"];
[label1 setTextColor:[UIColor colorWithRed:0.0 green:0.8 blue:0.0 alpha:1.0]];
uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(uploadGPS_tick:) userInfo:nil repeats:YES];
[self uploadGPS_tick:nil];
}else{
[label1 setText:#"Engaged"];
[label1 setTextColor:[UIColor colorWithRed:0.8 green:0.0 blue:0.0 alpha:1.0]];
if([uploadGPS_timer isValid]){
[uploadGPS_timer invalidate];
}
}
}
-(void)uploadGPS_tick:(NSTimer*)timer{
if(!lat || !lng){
//do nothing
}else{
NSString *urlStr=[[NSString alloc] initWithFormat:#"http://www.prestocab.com/driver/ajax/updateCoords.php"];
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
[request setPostValue:lat forKey:#"lat"];
[request setPostValue:lng forKey:#"lng"];
NSLog(#"EOH: %#",lat);
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *response=[request responseString];
NSLog(#"%#",response);
//do nothing
}];
[request setFailedBlock:^{
//NSError *error =[request error];
//do nothing
}];
[request startAsynchronous];
}
}
-(void)locationManager:(CLLocationManager*) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation*) oldLocation{
lat=[[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.latitude];
lng=[[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.longitude];
NSLog(#"%#,%#",lat,lng);
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
self.navigationItem.title=#"PrestoCab";
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStylePlain target:self action:#selector(logout:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
//GPS
lm=[[CLLocationManager alloc] init];
if([CLLocationManager locationServicesEnabled]){
lm.delegate=self;
lm.desiredAccuracy=kCLLocationAccuracyBest;
lm.distanceFilter=30.0f;
[lm startUpdatingLocation];
}
//[self check4messages_tick:nil]; //want to start immediately, not in 10/40 seconds' time
[self uploadGPS_tick:nil];
//check4messages_timer=[NSTimer scheduledTimerWithTimeInterval:40.0 target:self selector:#selector(check4messages_tick:) userInfo:nil repeats:YES];
uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(uploadGPS_tick:) userInfo:nil repeats:YES];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
if([uploadGPS_timer isValid]){
[uploadGPS_timer invalidate];
}
}
-(void)dealloc{
[super dealloc];
[uploadGPS_timer release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Also, here is
MyClassViewController.h:
#import <UIKit/UIKit.h>
#interface MyClassViewController : UINavigationController{
IBOutlet UIButton *passenger;
IBOutlet UIButton *driver;
}
#property (nonatomic,retain) UIButton *passenger;
#property (nonatomic,retain) UIButton *driver;
-(IBAction) passengerClicked:(id)sender;
-(IBAction) driverClicked:(id)sender;
#end
and MyClassViewController.m:
#import "MyClassViewController.h"
#import "MainMenuDriver.h"
#implementation MyClassViewController
#synthesize passenger;
#synthesize driver;
-(IBAction)passengerClicked:(id)sender{
NSLog(#"passenger");
}
-(IBAction)driverClicked:(id)sender{
NSLog(#"driver");
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:mainMenuDriver animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I can see a couple of things:
Your nib file's name is different from the name of your class. If you put nil, the UIViewController will try to load a nib file with the UIViewController's name. Your UIViewController can be MainMenuDriver but your nib file name can be MainMenuDriverNibFileName.nib
[self navigationController] is nil.
For this, do the following:
NSLog(#"hi");
if([self navigationController]){
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:mainMenuDriver animated:YES];
}
else{
NSLog(#"Houston we have a problem");
}
Update 1:
So, its nil, what you can do is the following:
Quick and Dirty:
[self presentViewController:mainMenuDriver animated:YES];
Instead of UIViewController switch to a UINavigationController.
Related
In "FirstViewController" I declare a button which present the modal view "InfoViewController".
In "InfoViewController", I declare a toolbar with a "modalViewButton" UIButton which dismiss the modal view. But the "OK" UIButton doesn't work. I don't know why.
Here's FirstViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface FirstViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
Here's FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize infoViewController;
- (IBAction)modalViewAction:(id)sender
{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[infoViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[modalViewButton addTarget:self
action:#selector(modalViewAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.leftBarButtonItem = modalBarButtonItem;
[modalBarButtonItem release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Here's InfoViewController.h
#import <UIKit/UIKit.h>
#interface InfoViewController : UIViewController
{
}
-(IBAction)infoDismissAction:(id)sender;
#end
Here's the InfoViewController.m
#import "InfoViewController.h"
#implementation InfoViewController
- (IBAction)infoDismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *infoLabel = [[UILabel alloc] init];
infoLabel.frame = CGRectMake(50, 100, 100, 40);
infoLabel.textAlignment = UITextAlignmentCenter;
infoLabel.text = #"About";
[self.view addSubview:infoLabel];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"OK"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(infoDismissAction:)];
UIBarButtonItem* infoTitle = [[UIBarButtonItem alloc] initWithTitle:#"About"
style:UIBarButtonItemStylePlain
target:self action:nil];
NSArray *barButtons = [[NSArray alloc] initWithObjects:flexibleSpace,flexibleSpace,infoTitle,flexibleSpace,doneButton,nil];
[toolBar setItems:barButtons];
[self.view addSubview:toolBar];
[toolBar release];
[infoTitle release];
[doneButton release];
[barButtons release];
[infoLabel release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I would solve this issue with a delegate method.
First make a protocol in your modalViewController
#protocol ModalViewDelegate <NSObject>
- (void)didDismissModalView;
#end
And set a delegate property in the same modalVC:
id<ModalViewDelegate> dismissDelegate;
Then make a buttonActionMethod that calls the delegate in the modalVC:
- (void)methodCalledByButton:(id)sender
{
// Call the delegate to dismiss the modal view
[self.dismissDelegate didDismissModalView];
}
Now your modalVC is done you have to prepare the mainVC calling the modalVC:
You have to make your MainViewController comform to the delegate:
#interface MainViewController : UIViewController <ModalViewDelegate>
At the place you alloc your ModalViewController you have to set the delegate property you made in your modalViewController:
self.myModalViewController.dismissDelegate = self;
Now the MainViewController listens to the delegate and the only thing you need to do is implement the delegateMethod.
-(void)didDismissModalView
{
[self dismissModalViewControllerAnimated:YES];
}
Now your ModalVC will dismiss on a buttonpress (at least when you call the method properly)
Hope this all makes sense.
Good luck.
You can only dismiss currently displayed modal view, so in your method infoDismissAction: you should do one of following
1) [self dismissModalViewControllerAnimated:YES];
2) Send to parent view controller message that current modal view should be dismissed and send reference to that view.
Second approach is better as it is more safe.
In your -infoDismissAction try to call [self dismissModalViewControllerAnimated:YES];
Here the best sample code for the model view for iphone and ipad also.
The popups have a number of configurable items. They can be animated to either slide or popup onto the display. Once visible, they can be dismissed either by tapping the screen or after a programmed delay. The background and text colors can also be adjusted however you like.
Download the sample code from here.
The current answers are deprecated. Here is the updated code:
[self dismissViewControllerAnimated:NO completion:nil];
I having a problem with a Bad Exception that I could not locate at first, but now have it pinned down on a [super dealloc];, but I have no idea why this happens.
Here is my code :
EditingViewController.h
#interface EditingViewController : UIViewController
{
NSManagedObject *editedObject;
NSString *editedFieldKey;
NSString *editedFieldName;
}
#property (nonatomic, retain) NSManagedObject *editedObject;
#property (nonatomic, retain) NSString *editedFieldKey;
#property (nonatomic, retain) NSString *editedFieldName;
- (IBAction)cancel;
- (IBAction)save;
#end
EditingViewController.m
#import "EditingViewController.h"
#implementation EditingViewController
#synthesize editedObject, editedFieldKey, editedFieldName;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
self.title = NSLocalizedString(editedFieldName, nil);
// Configure the save and cancel buttons.
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(save)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (IBAction)save
{
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)cancel
{
// Don't pass current value to the edited object, just pop.
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc
{
[editedObject release];
[editedFieldKey release];
[editedFieldName release];
//[super dealloc];
}
#end
As you can see, I commented the [super dealloc];, which is causing the Bad Exception, but this is obviously not a good solution.
Any idea what I am doing wrong ?
Thanks
The properties might have never been used, so they haven't been initialized in any way
- (void)dealloc
{
self.editedObject = nil;
self.editedFieldKey = nil;
self.editedFieldName = nil;
[super dealloc];
}
Hi I am a beginner in programming
I have already created a tapping application, displaying the tap count after pressing the result button
I want to add a NSTimer, counting 30 second after the first tap (after the tap button was pressed for the first time).
at the same time, displaying the time count down on a label (UILabel timeLabel)
and after 30 second, the tap count will restart to 0.
Please kindly tell me if I need to post anything other than the following, Thanks!!
Here is my .h file
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
#class Player;
#interface Tapping2ViewController : UIViewController
<AVAudioPlayerDelegate>
{
Player *aPlayer;
IBOutlet UILabel *timerLabel;
IBOutlet UILabel *resultLabel;
AVAudioPlayer *buttonPlayer;
NSTimer *lv1Timer;
NSInteger *counter1;
}
- (IBAction)addTap:(id)sender;
- (IBAction)getResult:(id)sender;
-(void)restartTapCount;
-(void)start;
#property (retain) NSTimer *lv1Timer;
#property (nonatomic, retain) IBOutlet UILabel *timerLabel;
#end
and my .M file
#import "Tapping2ViewController.h"
#import "Player.h"
#implementation Tapping2ViewController
#synthesize lv1Timer;
#synthesize timerLabel;
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)dealloc
{
[resultLabel release];
[lv1Timer release];
[aPlayer release];
[timerLabel release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
aPlayer = [[Player alloc] init];
[super viewDidLoad];
}
- (IBAction)addTap:(id)sender
{
//呢到係設定聲音, 首先要用NSSTRING 去 SET 左條路徑先
NSString *buttonFile = [[NSBundle mainBundle] pathForResource:#"button" ofType:#"wav"];
//之後再條NSSTRING 轉做NSURL (因為AVPLAYER 只認URL)
NSURL *buttonFileURL = [NSURL fileURLWithPath:buttonFile];
NSError *error = nil;
//設定AUDIO PLAYER 要播邊條 聲音 *記得SET DELEGATE 做自已去執行
buttonPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:buttonFileURL error:&error];
[buttonPlayer setDelegate:self];
NSLog(#"Before: %d", aPlayer.tapCount);
aPlayer.tapCount++;
//呼叫播放既METHOD
[buttonPlayer play];
NSLog(#"After: %d", aPlayer.tapCount);
/*
//即時顯示數字
aPlayer.result = aPlayer.tapCount;
NSString *sResult = [NSString stringWithFormat:#"%D", aPlayer.result];
resultLabel.text = sResult;
*/
}
- (IBAction)getResult:(id)sender {
aPlayer.result = aPlayer.tapCount;
NSString *aResult = [NSString stringWithFormat:#"%D", aPlayer.result];
resultLabel.text = aResult;
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
aPlayer.tapCount = 0;
resultLabel.text = #"0";
}
}
- (void)viewDidUnload
{
[resultLabel release];
resultLabel = nil;
[timerLabel release];
timerLabel = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//下面係PART OF DETECT SHAKE 既METHOD
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated
{
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
//去到呢到都係
#end
Here is how I do it in an app, on start :
count = COUNTDOWN_DURATION;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self selector:#selector(countDown)
userInfo:nil repeats:YES];
this will call a countDown method every second. Do whatever you want in that countDown method but make sure to stop the NSTimer on completion (and of course to decrement counter):
if (count < 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
...
count--;
Everything works ok on simulator , but on the real iphone 3g there is bug.
I call Splash Screen twice. First time - ok , second time i see the bug : http://img413.imageshack.us/i/bugicu.png/
How can i fix it ?
#interface iStateGameAppDelegate : NSObject
{
UIWindow* window;
UINavigationController* navigationController;
}
-(void) showSpalshScreen;
-(void) showSwitchViewController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
#implementation iStateGameAppDelegate
#synthesize window;
#synthesize navigationController;
- (BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
[self showSpalshScreen];
[window makeKeyAndVisible];
return YES;
}
-(void) showSpalshScreen
{
SplashScreen* splashScreen = [[[SplashScreen alloc] initWithNibName:#"SplashScreen" bundle:[NSBundle mainBundle]] autorelease];
[window addSubview:[splashScreen view]];
}
-(void) showSwitchViewController
{
SplashScreen* splashScreen = [[[SplashScreen alloc] initWithNibName:#"SplashScreen" bundle:[NSBundle mainBundle]] autorelease];
[window addSubview:[splashScreen view]];
}
- (void) dealloc
{
[navigationController release];
[window release];
[super dealloc];
}
#interface SplashScreen : UIViewController {}
#end
#implementation SplashScreen
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
NSTimer *timer = [[NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(timerFired:) userInfo:nil repeats:NO] retain];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)timerFired:(NSTimer *)timer
{
[[UIApplication sharedApplication] setStatusBarHidden:FALSE];
iStateGameAppDelegate* app = [UIApplication sharedApplication].delegate;
[app showSwitchViewController];
[[self view] removeFromSuperview];
[timer invalidate];
[timer release];
timer = nil;
}
- (void)dealloc {
[super dealloc];
}
Make sure the view's delegate is correctly set.
You should probably check for both UIInterfaceOrientationLandscapeRight and UIInterfaceOrientationLandscapeLeft.
Well, i have 2 view controllers the fist has three buttons each one represents an image on an array of images. When a user presses a button moves on the second view controller which has just an IBOutlet UIImageView to view the image. How can i do this programming trick using the UIImagePickerController? here are my files:
// PhotoListViewController.h
#import <UIKit/UIKit.h>
#import "PhotoDetailViewController.h"
#interface PhotoListViewController : UIViewController {
IBOutlet UIButton *button;
IBOutlet UIImageView *image1;
IBOutlet UIImageView *image2;
IBOutlet UIImageView *image3;
IBOutlet UILabel *label1;
IBOutlet UILabel *label2;
IBOutlet UILabel *label3;
IBOutlet UILabel *nameMikro1;
IBOutlet UILabel *nameMikro2;
IBOutlet UILabel *nameMikro3;
NSString *nameMikroProp;
IBOutlet PhotoDetailViewController *photoDetailViewController;
}
#property (nonatomic, retain) PhotoDetailViewController *photoDetailViewController;
-(IBAction)showImage:(id)sender;
#property (copy) NSString *nameMikroProp;
#end
// PhotoListViewController.m
#import "PhotoListViewController.h"
#import "PhotoDetailViewController.h"
#implementation PhotoListViewController
#synthesize nameMikroProp;
#synthesize photoDetailViewController;
-(IBAction)showImage:(id)sender{
photoDetailViewController = [[PhotoDetailViewController alloc]init];
//photoDetailViewController.delegate = self;
if([sender tag] == 4){
//[photoDetailViewController.imageViewprop setImage:[UIImage imageNamed:#"zaab.png"]];
//[self presentModalViewController:self.imgPicker animated:YES];
}
else if([sender tag] == 5){
}
else{
}
[self.navigationController pushViewController:photoDetailViewController animated:YES];
[photoDetailViewController release];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main" ofType:#"jpg"]]];
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main2" ofType:#"jpg"]]];
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main3" ofType:#"jpg"]]];
NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:#"zaab's photo one"];
[nameArray addObject:#"zaab's photo two"];
[nameArray addObject:#"zaab's photo three"];
nameMikro1.text = nameMikroProp;
nameMikro2.text = nameMikroProp;
nameMikro3.text = nameMikroProp;
if([#"zaab" isEqualToString:nameMikro1.text]) {
[image1 setImage:[imageArray objectAtIndex:0]];
[image2 setImage:[imageArray objectAtIndex:1]];
[image3 setImage:[imageArray objectAtIndex:2]];
[label1 setText:[nameArray objectAtIndex:0]];
[label2 setText:[nameArray objectAtIndex:1]];
[label3 setText:[nameArray objectAtIndex:2]];
}
else if([#"evza" isEqualToString:nameMikro1.text]) {
[image1 setImage:[UIImage imageNamed:#"evza.jpg"]];
}
[imageArray release];
[nameArray release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[nameMikro1 release];
[nameMikro2 release];
[nameMikro3 release];
[label1 release];
[label2 release];
[label3 release];
[image1 release];
[image2 release];
[image3 release];
[super dealloc];
}
#end
// PhotoDetailViewController.h
#import <UIKit/UIKit.h>
#interface PhotoDetailViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIImageView *imageViewprop;
UIImagePickerController *imgPicker;
}
#property (nonatomic, retain) UIImageView *imageViewprop;
#property (nonatomic, retain) UIImagePickerController *imgPicker;
- (IBAction)grabImage;
#end
// PhotoDetailViewController.m
#import "PhotoDetailViewController.h"
#implementation PhotoDetailViewController
#synthesize imageViewprop;
#synthesize imgPicker;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
imageViewprop.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"main" ofType:#"jpg"]];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (IBAction)grabImage {
[self presentModalViewController:self.imgPicker animated:YES];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = NO;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Why do you need an image picker controller to show an image? ImagePickerController is used to pick an image from the album or by using camera.
What you can do is to pass the image from the PhotoListViewController to PhotoDetailViewController.
In the PhotoDetailViewController, you can have a property of UIImage (synthesized), so that you can set it before loading it. Then in the PhotoDetailVC use a UIImageView to show the image.
If you are looking for more advance functionality with image, look at the three20 framework at
three20 at github.