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;
}
}
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.
In my app, there are different buttons that trigger different alert views. Each one has an option to click "More Information" which directs to the same UIWebView. I want to load a different url in the webview depending on which alert directed it to the webview.
I tried creating different segue identifiers and have attached that code. But the webview is blank/doesn't load the url. Is there a way to make this work using different segue identifiers? Or is there a better way to do this where I can use the same segue for all of the alerts but still load different urls in the webview? How do I tell the webview which one to load?
HomeViewController.m
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1){
if (alertView.tag==1) {
[self moreInfo];
}
}
}
-(void) moreInfo{
[self performSegueWithIdentifier:#"morebainfo" sender:nil];
}
-(void)ftmoreinfo{
[self performSegueWithIdentifier:#"moreftinfo" sender:nil];
}
- (IBAction)bainfo:(id)sender {
UIAlertView *baAlert =[[UIAlertView alloc] initWithTitle:#"Bacillus anthracis"
message:#"This is the message."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"More Information", nil];
baAlert.tag=1;
[baAlert show];
}
- (IBAction)ftinfo:(id)sender {
UIAlertView *ftAlert =[[UIAlertView alloc] initWithTitle:#"Francisella tularensis"
message:#"This is the message."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"More Information", nil];
[ftAlert show];
}
WebViewController.m
#interface MoreInfoViewController ()
#end
#implementation MoreInfoViewController
#synthesize MoreInfoWeb;
#synthesize loadUrl;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString: #"morebainfo"]){
MoreInfoViewController *webview = [segue destinationViewController];
webview.loadUrl = #"http://example.com/";
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *urlToLoad = [NSURL URLWithString:self.loadUrl];
[self.MoreInfoWeb loadRequest:[NSURLRequest requestWithURL:urlToLoad]];
}
Here's how I ended up fixing it:
I moved the prepare for segue method to the initial view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"prepareForSegue: %#", segue.identifier);
if ([segue.identifier isEqualToString: #"morebainfo"]){
MoreInfoViewController *webview = [segue destinationViewController];
webview.loadUrl = #"http://www.cdc.gov/anthrax/";
}
and then I gave each alert it's own tag:
- (IBAction)bainfo:(id)sender {
UIAlertView *baAlert =[[UIAlertView alloc] initWithTitle:#"Title"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"More Information", nil];
baAlert.tag=1;
[baAlert show];
}
Each tag called a different method:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1){
if (alertView.tag==1) {
[self moreInfo];
}
}
The methods performed the segues:
-(void) moreInfo{
[self performSegueWithIdentifier:#"morebainfo" sender:nil];
}
Fianlly, in the viewdidLoad of the webviewcontroller, I added:
NSURL *WebUrl = [NSURL URLWithString:self.loadUrl];
NSURLRequest *WebRequest = [NSURLRequest requestWithURL:WebUrl];
[MoreInfoWeb loadRequest:WebRequest];
And that worked! the main thing that helped was moving the prepare for segue method to the initial view controller instead of the webview controller
I have to navigate to next screen on button click using segue,but the condition is i call a web service ,if the result from web service is SUCCESS it should navigate else stay in the loginscreen. But using the below code it simply navigates ..
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:#"loginbutton"])
{
[self.sharedController setLoginDetailsDyanamic1:_strUsername.text password:_strPassword.text delegate:self];
if ([[dictresult objectForKey:#"Response"] isEqual: #"Success"])
{
DashBoardViewController *obj = [[DashBoardViewController alloc]init];
[self.navigationController pushViewController:obj animated:YES];
}
}
}
//delegates
-(void)controllerDidFinishLoadingWithResult:(id)result;
{
dictresult = result;
NSLog(#" result ----- :%#",dictresult);
NSLog(#" result of key----- :%#",[dictresult objectForKey:#"Response"]);
// if ([[result objectForKey:#"Response"] isEqual: #"Success"])
// {
// DashBoardViewController *obj = [[DashBoardViewController alloc]init];
// [self.navigationController pushViewController:obj animated:YES];
//
// }
}
-(void)controllerDidFailLoadingWithError:(NSError*)error;
{}
The result that i get in -(void)controllerDidFinishLoadingWithResult:(id)result is:
result ----- :{
Response = Success;
Token = 2e8c0ef66a5ac15b8f61da080c26d056218a6172;
errorCode = 0; }
How do i manage this? I have wired my button with the next view.
You have to set a Segue Identity on your storyboard then use this line of code to Segue to the next view controller.
[self performSegueWithIdentifier:#"SegueIdentity" sender:self];
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;
}
}