I'm having some difficulty figure out what I'm doing wrong when trying to assign my a delegate for my UIPopoverView. I tried to work around not even using one, but having it would be much more straightforward and clean. Here is the code that I think should cover it:
//.h of View where I call popover, this would be the delegate.
#import <UIKit/UIKit.h>
#import "ACTypePopoverViewController.h"
#interface NewRouteViewController : UIViewController<ACTypePickerDelegate>{
ACTypePopoverViewController *_acTypePicker;
UIPopoverController *_acTypePickerPopover;
}
#property (nonatomic, retain) ACTypePopoverViewController *acTypePicker;
#property (nonatomic, retain) UIPopoverController *acTypePickerPopover;
#end
//.m file for where I would like to use the popover, is the .m for the .h above
if (_acTypePickerPopover == nil)
{
ACTypePopoverViewController* content = [[ACTypePopoverViewController alloc] init];
UIPopoverController* aPopover = [[UIPopoverController alloc]
initWithContentViewController:content];
aPopover.delegate = self;
[content release];
// Store the popover in a custom property for later use.
self.acTypePickerPopover = aPopover;
}
[self.acTypePickerPopover presentPopoverFromRect:self.selectACTypeButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
//.h file for the actual popover, what I would be setting the delegate of
#protocol ACTypePickerDelegate
- (void)acTypeSelected:(NSString *)acType;
#end
#interface ACTypePopoverViewController : UITableViewController {
NSMutableArray *_acTypes;
NSString *selectedACType;
id<ACTypePickerDelegate> _delegate;
}
#property (nonatomic, retain) NSMutableArray *acTypes;
#property (nonatomic, retain) NSString *selectedACType;
#property (nonatomic, assign) id<ACTypePickerDelegate> delegate;
#end
I think thats all I need, but let me know if more code is needed!
Thanks!
I understood you correctly... what you need is:
content.delegate = self;
Right after this line you have:
ACTypePopoverViewController* content = [[ACTypePopoverViewController alloc] init];
Are you synthesizing your properties? Also you are assigning your delegate before initiating the popover...
#synthesize acTypePickerPopover;
self.acTypePickerPopover = [[[UIPopoverController alloc]
initWithContentViewController:_acTypePickerPopover] autorelease];
self.acTypePickerPopover.delegate = self;
`
Related
I'm trying to change the text of a UILabel with text from an array upon a button click, but it doesn't do anything.
#interface Test01AppDelegate : NSObject <UIApplicationDelegate> {
UILabel *helloLabel;
UIButton *hellobutton;
NSMutableArray *madWords;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIButton *hellowButton;
#property (nonatomic, retain) IBOutlet UILabel *hellowLabel;
#property (nonatomic, retain) NSMutableArray *madWords;
- (void) madArrays;
- (IBAction)helloYall;
#end
and
#import "Test01AppDelegate.h"
#implementation Test01AppDelegate
#synthesize window = _window;
#synthesize hellowButton;
#synthesize hellowLabel;
#synthesize madWords;
- (void) madArrays {
[madWords addObject:#"Part A"];
[madWords addObject:#"Part B"];
[madWords addObject:#"Part C"];
[madWords addObject:#"Part D"];
}
- (IBAction)helloYall {
[self madArrays];
self.hellowLabel.text = [madWords objectAtIndex:0];
}
I can set the helloLabel text with
#"some text here";
and it works fine. Also, I tried copying the "madArrays" method into the "helloYall" method and it still didn't work. As I said, I can manually set the text and it works, but I'd like to pull the info from an array. Eventually, I'd like to loop through the array to grab the text on each button press, but one step at a time. Thanks.
You never create the madWords array. You need to add:
self.madWords = [NSMutableArray array];
at the top of:
- (void) madArrays {
would probably be a good place. Other possibly good places would be i the class init method or the view controller viewWillAppear method.
// Or you can try this in your init Method:
//first allocate the ivar
- (void)myInitMethod {
madArrays = [[NSMutableArray]alloc]init];
}
//then you can do anything directly to the ivar or throughout de setter
- (void)doAnythingWithiVar {
// do your stuff
}
//when you are done you can dealloc your ivar
- (void)dealloc {
[madArrays release];
[super dealloc];
}
It looks like madArrays is still nil when you come to populate it. At some point you need something like [self setMadArrays:[[NSMutableArray alloc] init]];. Also, don't forget to release madArrays in the dealloc before calling super as you'll have a memory leak.
i am having a view consisting of a tableview and a uibutton like this
when i click on button "add" a new class/xib named "Locsetting" should be open.which i am not able to do.
my code: .h file
#interface LocationViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
UITableView *table;
NSMutableArray *menuList;
LocSetting *locSetting;
IBOutlet UIButton *addbutton;
}
#property (nonatomic,retain) NSMutableArray *menuList;
#property (nonatomic,retain) IBOutlet UITableView *table;
#property (nonatomic,retain) LocSetting *locSetting;
#property (nonatomic, retain) IBOutlet UIButton *addbutton;
-(IBAction)gotoLocSetting:(id) sender;
#end
My .m :
#synthesize addbutton;
-(IBAction)gotoLocSetting:(id) sender {
NSLog(#"gotoLocSetting Entered");
locSetting = [[LocSetting alloc]
initWithNibName:#"LocSetting"
bundle:nil];
//locationViewController.menuList = [menuList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:locSetting
animated:YES];
[locSetting release];
}
what wrong i am doing?or please guide me!
Thanks
On a quick look, there might be one of the following 3 problems:
Is your IBAction linked to the button's outlet?
If not, try removing LocSetting *locSetting; and its property/synthesize. Next, change your line down there to: Locsetting *locSetting = [[LocSetting alloc] initWithNibName:#"LocSetting" bundle:nil];
If that doesn't work, try changing that to: Locsetting *locSetting = [[LocSetting alloc] initWithNibName:nil bundle:nil];
Make sure the IBAction is connected properly to the button. And I think its better to have IBOutlet UIButton *addbutton; alone and #property (nonatomic, retain) UIButton *addbutton;
OK, i'm making an app that pulls down rss feeds, the initial table view is the headlines, the second view shows the detail of that headline the user selected.
This is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[self appDelegate] setCurrentlySelectedBlogItem:[[[self rssParser]rssItems]objectAtIndex:indexPath.row]];
NewsDetailViewController *newsDetail = [[NewsDetailViewController alloc] initWithNibName:#"NewsDetailViewController" bundle:nil];
//NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
// ...
// Pass the selected object to the new view controller.
newsDetail.titleTextView.text = [[[self appDelegate] currentlySelectedBlogItem]title];
newsDetail.descriptionTextView.text = [[[self appDelegate] currentlySelectedBlogItem]description];
[self.navigationController pushViewController:newsDetail animated:YES];
[newsDetail release];
}
setCurrentlySelectedBlogItem is a pointer to blogrss class which is reference in the AppDelegate. When i'm clicking on the row of the headline and its taking me to the next view, the content is empty. In the last two lines above i'm attempting to set the UILabel's as the current headline's details...
Anyone hazard a guess to why this is happening?
Also this is my AppDelegate.h file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#class RssFunViewController;
#class NewsDetailViewController;
#class RootViewController;
#class BlogRss;
#class BlogRssParser;
#interface ExampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
RootViewController *viewController;
NewsDetailViewController * newsDetailController;
BlogRss * currentlySelectedBlogItem;
#private
NSManagedObjectContext *managedObjectContext_;
NSManagedObjectModel *managedObjectModel_;
NSPersistentStoreCoordinator *persistentStoreCoordinator_;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *viewController;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet NewsDetailViewController * newsDetailController;
#property (readwrite,retain) BlogRss * currentlySelectedBlogItem;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSString *)applicationDocumentsDirectory;
- (void)saveContext;
#end
The UILabels in the view controller are probably still nil at that point and not accessible. Instead of setting the controls directly, create two NSString properties in the NewsDetailViewController and set those before you push it. Then, in the NewsDetailViewController, set the labels to the property values in the viewDidLoad method (at which point the labels should be instantiated and accessible).
I have a MyAppAppDelegate, it contains a window, and a UITabBarController.
#interface MyAppAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UITabBarController *rootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#end
And I have View A, that contain a button to switch to View B. It is the .h file:
#import <UIKit/UIKit.h>
#class MyAppAppDelegate;
#class ViewBController;
#interface ViewAController : UIViewController {
IBOutlet UIView *view;
IBOutlet UIButton *switchToViewBButton;
}
#property (retain, nonatomic) UIView *view;
#property (retain, nonatomic) UIButton *switchToViewBButton;
-(IBAction) startSwitching: (id)sender;
#end
And it is the.m file:
#import "ViewAController.h"
#import "ViewBController.h"
#import "MyAppAppDelegate.h"
#implementation ViewAController
/*skip the default generated codes*/
-(IBAction) startClock: (id)sender{
NSLog(#"Start Switching");
[rootController presentModalViewController:ViewBController animated:YES];
}
Plz notice that the ViewB is not enable to display on UITabBarController, it only appear, when the ViewA button is clicked. Also, I found that the debugger tell me that the rootController is undeclared. but I already import MyAppDelegate to the file. thz a lot... ...
You need to synthesize the rootController instance:
#synthesize rootController;
Then it should work. Put this line of code below the implementation line in the .m file. There is no reason why you should be getting the second error, so try my solution and then tell us what happened.
Also, please try to write in complete sentences. In my experience, if you write well in a forum post, you will gain more respect from people who might help you.
No you need to do something like this:
ViewBController* vc = [[ViewBController alloc] initWithNib: #"ViewBController" mainBundle: nil];
if (vc != nil) {
[rootController presentModalViewController: vc animated:YES];
[vc release];
}
The mistake that you are making is that you are passing presentModalViewController: the class of the ViewBController. Instead it needs an instance.
ViewBController* viewBController = [[[ViewBController alloc] initWithNibName: #"NameOfViewBControllerNibFile" bundle:nil] autorelease];
[self presentModalViewController:viewBController animated:YES];
You can not access rootController from ViewAController, because it is a property of MyAppAppDelegate, not ViewAController. If you want to access the UITabBarController in charge of ViewAController, then inside ViewAController you use self.tabBarController
So if you want the UITabBarController to do the above, change it to
ViewBController* viewBController = [[[ViewBController alloc] initWithNib: #"NameOfViewBControllerNibFile" mainBundle: nil] autorelease];
[self.tabBarController presentModalViewController:viewBController animated:YES];
ViewBController *vc = [[[ViewBController alloc] initWithNib:#"ViewBController"
mainBundle:nil] autorelease];
MyAppDelegate *appDelegate = (MyAppAppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate.rootController presentModalViewController:vc animated:YES];
I am trying to load a UIView with multiple data. The problem is data is not displays in first load but loads it in subsequent clicks. Can you help me trace the problem.
FirstViewController.h
#import <UIKit/UIKit.h>
#class SecondViewController;
#interface FirstViewController : UIViewController {
IBOutlet SecondViewController *mySecondViewController;
IBOutlet UIImageView *myimage1;
IBOutlet UIImageView *myimage2;
IBOutlet UILabel *mylabel1;
IBOutlet UILabel *mylabel2;
}
#property (nonatomic, retain) IBOutlet SecondViewController *mySecondViewController;
#property (nonatomic, retain) IBOutlet UIImageView *myimage1;
#property (nonatomic, retain) IBOutlet UIImageView *myimage2;
#property (nonatomic, retain) IBOutlet UILabel *mylabel1;
#property (nonatomic, retain) IBOutlet UILabel *mylabel2;
- (IBAction)bringNextView:(id)sender;
- (IBAction)bringNextView2: (id)sender;
#end
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation FirstViewController
#synthesize mySecondViewController,myimage1, myimage2, mylabel1, mylabel2;
- (IBAction)bringNextView:(id)sender {
mySecondViewController.name.text = #"Paul";
mySecondViewController.myimageview.image = [UIImage imageNamed:#"Paul.png"];
mySecondViewController.statmsg.text = #"Doing iPhone Development";
[[self navigationController] pushViewController:mySecondViewController animated:YES];
}
SecondViewController.m
#import "SecondViewController.h"
#implementation SecondViewController
#synthesize myimageview, name, statmsg
- (void)viewDidLoad {
[self setTitle:#"Details"];
[super viewDidLoad];
}
Properties are not coming for the first time. But are displays after returning from view and then loading view again.
I am loading SecondViewController from FirstViewController.
Hi I have solved this. Actually I was pushing my view controller after setting up the property and that caused this issue.
Instead of :
mySecondViewController.name.text = #"Paul";
mySecondViewController.myimageview.image = [UIImage imageNamed:#"Paul.png"];
mySecondViewController.statmsg.text = #"Doing iPhone Development";
[[self navigationController] pushViewController:mySecondViewController animated:YES];
It should be :
[[self navigationController] pushViewController:mySecondViewController animated:YES];
mySecondViewController.name.text = #"Paul";
mySecondViewController.myimageview.image = [UIImage imageNamed:#"Paul.png"];
mySecondViewController.statmsg.text = #"Doing iPhone Development";