Im trying to send some info from a modal view to a delgate. But it seems like my delegate doesnt follow through, it gets null. It looks like this in IB http://i.imgur.com/7oaxb.png.
But it works if i remove the navigationController that is right before the modal view and use the buttons in the View.
please help, ive tried for like 5 hours... :/
Heres the modalViewController code:
#import
#import "Link.h"
#protocol modalViewDelegate <NSObject>
-(void)closeview;
-(void)saveLink:(Link *)link;
#end
#interface modelViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *titel;
#property (weak, nonatomic) IBOutlet UITextField *url;
#property (nonatomic, weak) id <modalViewDelegate> delegate;
- (IBAction)exitModal:(id)sender;
- (IBAction)saveLink:(id)sender;
#end
and .m:
#import "modelViewController.h"
#interface modelViewController ()
#end
#implementation modelViewController
#synthesize titel;
#synthesize url, delegate;
- (IBAction)exitModal:(id)sender {
//[self.delegate closeview];
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)saveLink:(id)sender {
if (titel.text.length > 0 && url.text.length > 0) {
NSString *urlen = [NSString stringWithFormat:#"%#", url.text];
Link *linken = [[Link alloc] initWithURL:[NSURL URLWithString:urlen]];
linken.title = titel.text;
NSLog(#"%#", delegate); **//returns null when pressing button** it returns null if i put it in viewDidLoad to..
[self.delegate saveLink:linken];
[self dismissModalViewControllerAnimated:YES];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:#"warning" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alertView show];
}
}
#end
the MasterViewController .h (that pushes the modalview:
#import <UIKit/UIKit.h>
#import "modelViewController.h"
#class DetailViewController;
#interface MasterViewController : UITableViewController <modalViewDelegate>
....
and .m
#import "MasterViewController.h"
#import "DetailViewController.h"
#implementation MasterViewController
#synthesize detailViewController = _detailViewController;
#synthesize links;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"linkPush"]) {
// Skicka länken till detaljvyn
DetailViewController *detailVC = segue.destinationViewController;
detailVC.link = [self.links objectAtIndex:self.tableView.indexPathForSelectedRow.row];
NSLog(#"%#", detailVC);
}
//this is the modalview "pusher"
if ([segue.identifier isEqualToString:#"newLink"]) {
modelViewController *mvc = segue.destinationViewController;
mvc.delegate = self;
NSLog(#"%#", mvc.delegate);
}
}
- (void)closeview {
[self dismissModalViewControllerAnimated:YES];
//[self.tabBarController dismissModalViewControllerAnimated:YES];
}
-(void)saveLink:(Link *)link{
NSLog(#"hello");
[links insertObject:link atIndex:links.count]; //updates a Tableview and works fine if delegate is called
[self.tableView reloadData];
//[self dismissModalViewControllerAnimated:YES];
}
If your destination view controller is wrapped into a navigation controller, you have to refer to it differently in prepareForSegue:
UINavigationController *nav = segue.destinationViewController;
DetailViewController *dvc = [nav.viewControllers objectAtIndex:0];
Now setting the properties, including the delegate, should work.
Related
this is my very first app and, basically, this part consists in passing data from a UItableView to a second View Controll. I managed to learn how to pass data from a simple NSarray (also in a UITable), but my goal is to pass values from a NSDictionary. Everything is set up, but I can't figure out how to write the PrepareForSegue method properly. The app runs, but the label on the "DetailView" stays empty. What I got so far:
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
_citySpots = #{#"Bars" : #[#"Hurricane", #"Black Swan", #"Texas"],
#"Clubs" : #[#"Electric Wizard", #"Offspring", #"The Tunnel"],
#"Restaurants" : #[#"Nando's", #"1/2 Burguer", #"Satellite"],
};
_sectionTitles = [[_citySpots allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
}
PrepareForSegue Method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"spotsDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *destViewController = segue.destinationViewController;
NSString *sectionTitle = [_sectionTitles objectAtIndex:indexPath.section];
NSArray *citySpots = [_citySpots objectForKey:sectionTitle];
destViewController.receiver = [citySpots objectAtIndex:indexPath.row];
}
}
And the receiver(header):
#import <UIKit/UIKit.h>
#import "TableViewController.h"
#interface DetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *receiver;
#property (nonatomic, strong) NSString *spot;
#end
Main:
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
_receiver.text =_spot;
}
Can someone help me out? Thanks
Try to use setters:
[destViewController setReceiver:[citySpots objectAtIndex:indexPath.row]];
[destViewController setSpot:[citySpots objectAtIndex:indexPath.row]];
I have two UIViewController class with names : (RootViewController & SecondViewController).
I have one UIButton Outlet in my SecondViewController.now I want give action method to my UIButton in RootViewController.but I don't know about it.
please guide me and tell me how to get my UIButton in another View and give action method on it in another View....
I'm not sure why you are doing this, it doesn't look good.
Anyway, here's the way.
SecondViewController.h
#interface SecondViewcontroller:UIViewController
#property (nonatomic, weak) IBOutlet UIButton *theButton;
#end
RootViewController.m
SecondViewContoller * sv = [[SecondViewController alloc] init];
[sv.theButton addTarget:self action:#selector(buttonHandler:) forControlEvents:UIControlEventTouchUpInside];
You have to removeTarget when you've done with the button.
Why don't you use delegate or block callback?
ADDED
delegate
SecondViewController.h
#protocol SecondViewControllerDelegate <NSObject>
- (void)theButtonPressed;
#end
#interface SecondViewController : UIViewController
#property (nonatomic, strong) id<SecondViewControllerDelegate>delegate;
#end
SecondViewController.m
#interface SecondViewController ()
{
UIButton *theButton;
}
#end
#implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[theButton addTarget:self.delegate action:#selector(theButtonPressed) forControlEvents:UIControlEventTouchUpInside];
}
#end
RootViewcontroller.h
#interface RootViewController : UIViewController <SecondViewControllerDelegate>
#end
RootViewController.m
SecondViewController *sv = [[SecondViewController alloc] init];
[sv setDelegate:self];
and
- (void)theButtonPressed
{
}
block
SecondViewController.h
typedef void(^TheButtonTouched)(void);
#interface SecondViewController : UIViewController
- (void)addButtonEvent:(TheButtonTouched)event;
#end
SecondViewController.m
#interface SecondViewController ()
{
UIButton *theButton;
TheButtonTouched buttonBlock;
}
#end
#implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// button alloc init here and..
[theButton addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonEvent:(UIButton *)sender
{
if(buttonBlock)
{
buttonBlock();
}
}
- (void)addButtonEvent:(TheButtonTouched)event
{
buttonBlock = event;
}
#end
RootViewController.m
SecondViewController *sv = [[SecondViewController alloc] init];
[sv addButtonEvent:^{
// ADD SOMTHING HERE
}];
You need to create the button's iboutlet property.
Than using object of secondclass.button name you can assign selector.
[classobjectname..btnname addTarget:objectname.class action:#selector(objectname.methodname:);
Just Create RootViewController 's Object and passed into target parameter.
e.g.
[btnShow addTarget:self.rootCtrlObj action:#selector(viewPhoto_click:)
forControlEvents:UIControlEventTouchUpInside];
where rootCtrlObj is #property inside SecondViewController.h file
and when you push this controller in RootViewController you have to pass reference of RootViewController .
For Example,
secVCObj.rootCtrlObj = self;
[self.navigationController pushViewController:secVCObj animated:YES];
as i said custom delegate methods will work perfect for these cases,
in SecondViewController.h file define a custom delegate method like below
#import <UIKit/UIKit.h>
#import "ViewController.h"
#protocol SecondControllerDelegate<NSObject>
- (void)someActionToPerformInRootController;
#end
#interface SecondViewController : UIViewController
#property (nonatomic, assign) id<SecondControllerDelegate>delegate; //declare a custom delegate
#end
in SecondViewController.m file lets take an action of some button
- (IBAction)myButtonActionMethod:(id)sender
{
if([self.delegate respondsToSelector:#selector(someActionToPerformInRootController)])
{
[self.delegate someActionToPerformInRootController]; //this will call the delegate method in first view conroller( root view controller) //do what ever u want t do in the action method of rootviewcontroller
}
}
in RootViewController.h file
#import "SecondViewController.h"
#interface RootViewController : UIViewController < SecondControllerDelegate> //confirmas to delegate
in RootViewController.m while presenting the SecondViewController set the delegate to self
- (IBAction)showAction:(id)sender
{
SecondViewController *secondController = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
secondController.delegate = self; //call back to this controller
[self presentViewController:secondController animated:YES completion:nil];
}
//this method called from the secondViewcontroller
- (void) someActionToPerformInRootController
{
NSLog(#"perform action in root view controller");
}
I'm a xcode noob, but here's what I'm looking for followed by what I've done. I've created a login and with that login usernameField I'm trying verify the user and open a only a web page specific to that person. The login is working here's all my code
//LoginViewController.h
#import <UIKit/UIKit.h>
#interface LoginViewController : UIViewController
{
IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;
IBOutlet UIButton *loginButton;
IBOutlet UIActivityIndicatorView *loginIndicator;
}
#property(nonatomic, strong) UITextField *usernameField;
#property(nonatomic, strong) UITextField *passwordField;
#property(nonatomic, strong) UIButton *loginButton;
#property(nonatomic, strong) UIActivityIndicatorView *loginIndicator;
-(IBAction) login: (id) sender;
#end
//LoginViewController.m
#import "LoginViewController.h"
#implementation LoginViewController
#synthesize usernameField, passwordField, loginButton, loginIndicator;
-(IBAction) login: (id) sender
{
if ([usernameField.text isEqualToString: #"userOne"] && [passwordField.text
isEqualToString: #"passwordOne"])
{
printf("Success")
//here is where I would like to pass usernameField.text to WebViews
}
else if (([usernameField.text isEqualToString: #"userTwo"] && [passwordField.text
isEqualToString: #"passwordTwo"])
{
printf("Success")
//here is where I would like to pass usernameField.text to WebViews
}
else
{
printf("Login Failed")
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Login Failed"
message:#"Wrong username and password" delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
[alert show];
}
loginIndicator.hidden=False;
[loginIndicator startAnimating];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"fromLogin"])
{
WebViews *wvc = [segue destinationViewController];
wvc.usernamerField = self.usernameField.text;
}
}
#end
//WebViews.h
#interface WebViews : UIViewController
{
IBOutlet UIWebView *webView;
NSString *usernameField;
}
#property (nonatomic, strong) NSString*usernameField;
#end
//WebViews.m
#import "WebViews.h"
#interface WebViews ()//To be honest I'm not sure what this is for
#end
#implementation WebViews
#synthesize usernameField;
-(void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"username is = %#", self.usernameField);
if([T.usernameField.text isEqualToString: #"userOne"])
{
[webView loadRequest: [NSURLRequest requestWithURL: [NSURL
URLWithString:#"http://www.google.com"]]];
}
else if([T.usernameField.text isEqualToString: #"userTwo"])
{
[webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:#"http://www.yahoo.com"]]];
}
else
{
[webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:#"http://www.wikipedia.com"]]];
}
#end
Any help is greatly appreciated
Your problem is that you are never assigning anything to T so it is nil in your method.
In the code for your LoginViewController you need to pass the username to the WebViewController. You can do this by implementing the prepareForSegue method.
Here is an example, taken from How to pass prepareForSegue: an object
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
WebViewController *wvc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
wvc.username = self.usernameField.text;
}
}
so i'm testing this so i can use it on my bigger project.
I have A tabbarcontroller named TabBar
this TabBar has 2 tabs every tab has a navigationcontroller. A viewController with a button (OkButtonViewController) when you click this button you go to the viewcontroller with the label (LabelViewController). The OkButton View Controller is always in portrait and the labelViewController can switch orientation. this works only in one situation it goes wrong. when you are in the LabelViewController, orientated in landscape, and you switch tabs the OkButtonViewController is also in landscape, and stays in landscape. How can i force the viewcontroll to go back to portrait?
here is my code.
I probably need to add something in the TabBar or in the RotatingTabBarAppDelegate. I just don't know what.
TabBar.m
#import "TabBar.h"
#implementation TabBar
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#end
RotatingTabBarAppDelegate.h
#import <Foundation/Foundation.h>
#import "TabBar.h"
#class RotatingTabBarAppViewController;
#interface RotatingTabBarAppDelegate : NSObject<UIApplicationDelegate>
{
IBOutlet UIWindow *window;
}
#property (nonatomic, strong) UIWindow *window;
#end
RotatingTabBarAppDelegate.m
#implementation RotatingTabBarAppDelegate
#synthesize window;
-(void) applicationDidFinishLaunching:(UIApplication *)application
{
UIViewController *tab1 = [[UIViewController alloc] init];
tab1.tabBarItem =[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:0];
UIViewController *tab2 = [[UIViewController alloc] init];
tab2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
TabBar *tbc = [[TabBar alloc] init];
[tbc setViewControllers:[NSArray arrayWithObjects:tab1, tab2, nil]];
[window addSubview:tbc.view];
[window makeKeyAndVisible];
}
#end
OkButtonViewController.h
#import <UIKit/UIKit.h>
#interface OkButtonViewContoller : UIViewController
- (IBAction)ok;
#end
OkButtonViewController.m
#import "OkButtonViewController.h"
#import "LabelViewController.h"
#define kDetailSegue #"Detail"
#implementation OkButtonViewContoller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)ok
{
[self performSegueWithIdentifier:kDetailSegue sender:#"test"];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:kDetailSegue]) {
((LabelViewController *)segue.destinationViewController).testTekst = sender;
}
}
#end
LabelViewController.h
#import <UIKit/UIKit.h>
#interface LabelViewController : UIViewController
#property (nonatomic, strong) NSString *testTekst;
#property (nonatomic, strong) IBOutlet UILabel *testLabel;
#end
LabelViewController.h
#import "LabelViewController.h"
#implementation LabelViewController
#synthesize testTekst;
#synthesize testLabel;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.testLabel.text = testTekst;
}
#end
sorry for the bad english.
You cannot force a view to rotate, probably Apple would reject your app, you need to find another way to design this.
I'm Trying to dismiss a popover and transfer data at the same time. I implemented a delegate DismissPopoverDelegate but it is failing to work. But there are no errors. If the save button is tapped it registers it and it completes the line after where it calls the delegate. But its not working...
AddEventViewController_iPad.h
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <Foundation/Foundation.h>
#import "AboutSme.h"
#import "dateViewPopOverViewController_iPad.h"
#import "addPersonViewControllerPopover_iPad.h"
#import "PreviousEventsTableViewControllerPopover_iPad.h"
#interface AddEventViewController_iPad : UIViewController <UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate, UITextFieldDelegate, UIAlertViewDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate,ABPeoplePickerNavigationControllerDelegate, ABNewPersonViewControllerDelegate, DismissPopoverDelegate> {
UIPopoverController *pop;
AddEventViewController_iPad.m
- (IBAction) selectStartDate:(id) sender {
NSLog(#"Select start date");
dateViewPopOverViewController_iPad *dateViewPopOverViewController = [[dateViewPopOverViewController_iPad alloc] init];
popover2 = [[UIPopoverController alloc] initWithContentViewController:dateViewPopOverViewController];
popover2.delegate = self;
popover2.popoverContentSize = CGSizeMake(320, 460);
CGRect rect = CGRectMake(790, 170, 175, 300);
[popover2 presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
[dateViewPopOverViewController release];
/*
if (dateViewController == nil) {
dateViewController = [[DateViewController_iPad alloc] initWithNibName:#"DateViewController_iPad" bundle:nil];
}
[self presentModalViewController:dateViewController animated:YES];
[dateViewController retain];
*/
}
- (void) dismissWithData:(NSString *)data
{
NSLog(#"%#", data);
[pop dismissPopoverAnimated:YES];
[pop release];
}
dateViewPopOverViewController_iPad.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#protocol DismissPopoverDelegate <NSObject>
#required
- (void) dismissWithData:(NSString *)data;
#end
#interface dateViewPopOverViewController_iPad : UIViewController {
IBOutlet UIDatePicker *datePicker;
IBOutlet UISegmentedControl *segmentedBar;
IBOutlet UILabel *startLabel;
IBOutlet UILabel *endLabel;
IBOutlet UISwitch *allDaySwitch;
NSDate *startDate;
NSDate *endDate;
NSDate *now;
NSDateFormatter *dateFormatter;
id<DismissPopoverDelegate> delegate;
}
#property (retain) id delegate;
- (void) dismissWithData:(NSString *)data;
dateViewPopOverViewController_iPad.m
#implementation dateViewPopOverViewController_iPad
#synthesize startDate, endDate, datePicker, segmentedBar, startLabel, endLabel, now, allDaySwitch, delegate;
- (IBAction) save:(id)sender {
if ([self startDateIsValid] && [self endDateIsValid]) {
//[[self parentViewController] setDatesForEvent:startDate eventEndDate:endDate allDay:[allDaySwitch isOn]];
// [self dismissModalViewControllerAnimated:YES];
NSLog(#"works");
[self.delegate dismissWithData:#"Some text from popover"];
NSLog(#"works1");
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"Please check the dates! Remember the end date must occur after the start date for the event to save." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
You have a circular reference when including the .h files.
dateViewPopOverViewController_iPad.h includes AddEventViewController_iPad.h and AddEventViewController_iPad.h includes dateViewPopOverViewController_iPad.h, which will cause the compiler to raise an error.
One of the aims to use protocols is to avoid this kind of circular reference. Removing the dateViewPopOverViewController_iPad.h include from your AddEventViewController_iPad.h file might fix the problem
Your call to dismissPopoverAnimated will not trigger the call to the delegate. From Apple's UIPopoverDelegate documentation:
The popover controller does not call this method in response to programmatic calls to the dismissPopoverAnimated method. If you dismiss the popover programmatically, you should perform any cleanup actions immediately after calling the dismissPopoverAnimated method.