I am trying to apply state preservation & restoration in an iPad app but if a popover is open then, during restoration, it is briefly displayed and then hides. This is caused by execution of code in viewDidLoad of the main view controller where the popover is attached to. This code takes some time to execute and it loads an html file in a web view. As a result when the web view finally loads the html file, popover disappears. Can I prevent the popover from hiding without moving the viewDidLoad code elsewhere?
Looking forward for your help!
- (void)viewDidLoad
{
[super viewDidLoad];
text = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:&error];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webview loadHTMLString:text baseURL:baseURL];
}
This is the code that is run as a result of clicking on a bar button item.
However the problem appears when the app resumes with the popover previously left open.
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:#"filterPopover"]) {
if (self.filterPopover == nil && self.prefsPopover == nil) {
return YES;
}
return NO;
}
if ([identifier isEqualToString:#"prefsPopover"]) {
if (self.prefsPopover == nil && self.filterPopover == nil) {
return YES;
}
return NO;
}
return YES;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"filterPopover"]) {
// Assign popover instance so we can dismiss it later
self.filterPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
// assign delegate for popover dismiss purpose
self.filterPopover.delegate = self;
filterPopOverVisible = true;
}
if ([segue.identifier isEqualToString:#"prefsPopover"]) {
// Assign popover instance so we can dismiss it later
self.prefsPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
// assign delegate for popover dismiss purpose
self.prefsPopover.delegate = self;
}
}
Related
I have two web views in home tab which is home web view and login web view.I would like to pass value from login tab to home tab if I pressed login button action using segue.
If I pressed login button,I would like to go login web view and if not,I would like to show normal home web view.I created segue to pass value.But it doesn't pass.
My segue login button identifier is "loginsegue".
piece of my code viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
if([self.urlString isEqualToString:#"loginbtn"]){
NSLog(#"login button pressed");
[self loginCompleted];//call webview url from method
}else{
NSLog(#"home button pressed");
[self homeCompleted];//call webview url from method
}
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
ViewController * target = segue.destinationViewController;
if ([segue.identifier isEqualToString:#"loginsegue"]) {
target.urlString = #"loginbtn";
}
}
Since you haven't mentioned how you are loading home page. Sample i have used login button segue.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if([self.urlString isEqualToString:#"loginbtn"]){
NSLog(#"login button pressed");
[self performSeguewithidetifier#"loginsegue"];
}else{
NSLog(#"home button pressed");
[self performSeguewithidetifier#"loginsegue"];
}
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
ViewController * target = (ViewController*) segue.destinationViewController;
if ([segue.identifier isEqualToString:#"loginsegue"]) {
if([self.urlString isEqualToString:#"loginbtn"]){
target.urlString = #"LoginPageURL";
}
else{
target.urlString = #"homePageURL";
}
}
}
So when I press captured button, it's taking a photo and I can save the image in my camera roll. But when I want to see image in another view I can't see it.
This is my code :
- (IBAction)captureButton:(id)sender
{
[self.cameraViewController captureImageWithCompletionHander:^(id data) {
self.image = ([data isKindOfClass:[NSData class]]) ? [UIImage imageWithData:data] : data;
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
[self performSegueWithIdentifier:#"savingSegue" sender:self];
}];
}
And this is the prepare for segue method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = [[PhotoSaveViewController alloc] init];
[pvc.imageView setImage:self.image];
}
}
It sounds like your destination view controller is a UINavigationController. I'm guessing its top view controller is the one you want. You can access it like this (though you may wish to add type checking):
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
UINavigationController *navigationController = segue.destinationViewController;
PhotoSaveViewController *pvc = (PhotoSaveViewController *)navigationController.topViewController;
[pvc.imageView setImage:self.image];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"savingSegue"])
{
YourNextViewController*ImageView=segue.destinationViewController;
UIImage*clickedImage=capturedPicImageView.image;
ImageView.passedImage=clickedImage;
// NSLog(#"%#",editView.editPhotoImgView.image);
}
}
(And in YourNextViewController create UIImage*clickedImage and give property to it. and dont forget to allocate in viewDidLoad. Hope this helps.
I am pretty new to iOS. I have a UIViewController with an embedded UINavigationController and I am hiding the bar on the first View by adding this code to the viewDidLoad method:
if (self) {
self.navigationController.navigationBarHidden = YES;
self.navigationItem.title = #"";
}
Then I connected this View to another UIViewController with a seque (triggered by a button on the first View) and added this code to it's viewDidLoad method:
if (self) self.navigationController.navigationBarHidden = NO;
Everything works fine at first. When I load the app the navbar is gone, when I navigate to the second (child) view, the bar is there. Then when I hit the back button, it goes back to the first (parent) view but the NavBar is back.
I tried adding naming the seque "BackToMain" and adding a prepareSeque method which I placed in the second view:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"BackToMain"]) {
UINavigationController *parentNavigationController = segue.destinationViewController;
parentNavigationController.navigationController.navigationBarHidden = YES;
}
}
But it never gets called.
Ideally I would put a method in the parent view to simply hide the NavBar every time it gets displayed. Something like:
- (void)processIncomingSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (self) self.navigationController.navigationBarHidden = NO;
}
But I really don't know if that is possible.
Put the following code in your view controller which should not have a UINavigationBar.
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
I create Login page include (username & password & login button)
I want when to fill text field username and password if right user and password go on another page else if to be wrong give me one alert.
this is my code:
#import "Detail.h"
#implementation ViewController
{
NSString *name;
}
#synthesize Username,Password;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)Login:(id)sender {
NSString *a = [[NSString alloc]initWithFormat:#"http://192.168.1.102/mamal/login.php?u=%#&p=%#",Username.text,Password.text];
NSString *url = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:a]];
NSLog(#"%#",url);
if ([url isEqualToString:#"0"]) //this is wrong user & password
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"ERROR" message:#"WRONG" delegate:nil cancelButtonTitle:#"Dissmis" otherButtonTitles: nil];
[alert show];
}
else
{
name = #"Mohammad";
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([name isEqualToString:#"Mohammad"] , [[segue identifier] isEqualToString:#"Segue"]) {
Detail *det = segue.destinationViewController;
}
}
but dosent work!!! I want only right user & password go on next page
You have to make triggerless segue to perform this operation. Make triggerless segue from storyboard and fire it when your username and password is right.
To create a triggerless segue, start control+dragging the segue from the containing view controller icon in the scene dock at the bottom of the scene. Drag to the destination scene like normal, and pick the segue type. Select the segue, and in the inspector, choose a segue identifier.
Then call the method [self performSegueWithIdentifier:#"Identifier" sender:nil];
when your condition is true
If you want to use segues I think you are missing the call to actually perform the segue after the line where you set the name
name = #"Mohammed"
[self performSegueWithIdentifier:#"mySegue" sender:self]
Being "mySegue" the identifier of your segue in storyboard.
And then, you probably want to send the new View Controller the name.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"mySegue"]) {
Detail *det = segue.destinationViewController;
det.name = name;
}
}
Here AuditViewController is segue name
you need to call class like this in storyboard
[self performSegueWithIdentifier:#"AuditViewController" sender:self];
Edit
Check Condition
- (IBAction)Login:(id)sender {
NSString *a = [[NSString alloc]initWithFormat:#"http://192.168.1.102/mamal/login.php?u=%#&p=%#",Username.text,Password.text];
NSString *url = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:a]];
NSLog(#"%#",url);
if ([url isEqualToString:#"0"]) //this is wrong user & password
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"ERROR" message:#"WRONG" delegate:nil cancelButtonTitle:#"Dissmis" otherButtonTitles: nil];
[alert show];
}
else
{
name = #"Mohammad";
if([name isEqualToString:#"Mohammad"])
[self performSegueWithIdentifier:#"Segue" sender:self];
//else
//NSLog(#"Can not Login");
}
}
prepareForSegue method call
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"Segue"])
{
Detail *det = segue.destinationViewController;
}
}
I'm having a problem similar to this... Why am I seeing a black screen? Did I not alloc something?
Sorry for the beginner question but I'm not sure how to debug this. I'm seeing a blank (black) screen for one of my view controllers. When I set the view controller to "is initial view controller" in the inspector, it works as expected. However when I remove that (it shouldn't be the initial VC, just did that to debug) and navigate to the view, I get a blank (black) screen.
There is a webview in the VC (referenceDetailWebView) and when I try to log that out on the viewDidLoad of the VC and I get (null). Again, when I set the VC to be "initial", it logs out the referenceDetailWebView object as expected.
This is the implementation of the VC...
#implementation DetailViewController
#synthesize currentReference, referenceDetailWebView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"reference/accommodation" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[referenceDetailWebView loadHTMLString:htmlString baseURL:nil];
// log for debugging
NSLog(#"%#", [self referenceDetailWebView]);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The only thing that I can think of is that I might be missing something in my segues. I'm handling them programmatically and it's very possible that I'm overlooking something there...? My didSelectRowAtIndexPath looks like this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Reference *currentItem = [references objectAtIndex:indexPath.row];
NSString *mySegueID;
if([currentItem hasSubReferences]){
mySegueID = #"ShowSubTable";
} else {
mySegueID = #"ShowDetail";
}
[self performSegueWithIdentifier:mySegueID sender:nil];
}
And my performSegueWithIdentifier looks like this...
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
// the segue with identifier allows us to chose a segue programatically
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Reference *item = [references objectAtIndex:path.row];
if([identifier isEqualToString:#"ShowSubTable"]){
// looking for sub table
ReferencesSubTableViewController *subTableVC = [[ReferencesSubTableViewController alloc] initWithNibName:nil bundle:nil];
[subTableVC setCurrentReference:item];
[self.navigationController pushViewController:subTableVC animated:YES];
} else if ([identifier isEqualToString:#"ShowDetail"]){
// looking for detail view
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[detailVC setCurrentReference:item];
[self.navigationController pushViewController:detailVC animated:YES];
}
}
The segues are working as expected with the exception of that detailViewController displaying a blank screen and giving me null for my referenceDetailWebView.
I'm stuck. Any advice or guidance is very much appreciated! Thanks in advance for the help!!
Edit: It looks like the segues are probably the cause. When I remove the multiple segues and related code, and add a simple segue from the table cell to the DetailViewController only, the detailVC and views load correctly. Not sure what I'm missing in the segues though...?
I've simplified the performSegueWithIdentifier to the following and I'm still getting the blank screen...
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Reference *item = [references objectAtIndex:path.row];
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[detailVC setCurrentReference:item];
[detailVC setTitle:[item title]];
[self.navigationController pushViewController:detailVC animated:YES];
}
You're getting a black screen because you're instantiating your controller incorrectly. Also, you don't push the view controller with pushViewController:animated:, that's done for you when you call performSegueWithIdentifier:. If you have the segue setup in the storyboard, then you should be implementing prepareForSegue:sender:, not performSegueWithIdentifier:. Its code should look like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// the segue with identifier allows us to chose a segue programatically
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Reference *item = [references objectAtIndex:path.row];
if([segue.identifier isEqualToString:#"ShowSubTable"]){
// looking for sub table
ReferencesSubTableViewController *subTableVC = (ReferencesSubTableViewController *)segue.destinationViewController;
[subTableVC setCurrentReference:item];
} else if ([segue.identifier isEqualToString:#"ShowDetail"]){
// looking for detail view
DetailViewController *detailVC = (DetailViewController *)segue.destinationViewController;
[detailVC setCurrentReference:item];
}
}
You really need to read and understand Apple's docs on view controllers and segues -- you clearly have some fundamental misunderstanding of the way they work.