How can I add TTTableViewDragRefreshDelegate only for one page in UITabBarController? - objective-c

Hi I have a tabbar controller which includes two pages A and B. Now I just want to add TTTableViewDragRefreshDelegate to page A.
If I put the code:
- (id<TTTableViewDelegate>) createDelegate {
TTTableViewDragRefreshDelegate *delegate = [[TTTableViewDragRefreshDelegate alloc] initWithController:self];
return [delegate autorelease];
}
Into UITabBarController then both A and B got refresh, so I put above code into A's view controller and call A's view controller in UITabBarController like this:
if (_page == APage) {
AViewController *_aViewController = [[[AViewController alloc] init] autorelease];
self.dataSource = _aViewController.dataSource;
...
}
But seems not work, page A still not get drag and refresh. So how can I get TTTableViewDragRefreshDelegate only for page A in a UITabBarController?
This is AViewController init code:
- (id)init {
if (self = [super init]) {
self.variableHeightRows = YES;
id<TTTableViewDataSource> ds = [APageDataSource dataSourceWithItems:nil];
ds.model = CreateTabModelWithCurrentSettings();
self.dataSource = ds;
}
return self;
}
How to let make page A get all AViewController's properties? Like AViewController's model, dataSource, something like that?

AViewController should be a subclass of TTTableViewController

Related

Boolean from class X doesn't persist between change from view controller of class Y to view controller of class Z

In the class GHHaiku I have the BOOL property justComposed.
In mySecondViewControllerClass I have the following methods:
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.ghhaiku)
{
self.ghhaiku = [[GHHaiku alloc] init];
}
//Lots of other code
}
-(void)saveUserHaiku
{
//Lots of code
self.ghhaiku.justComposed=YES;
NSLog(#"%d",self.ghhaiku.justComposed);
[self.tabBarController setSelectedIndex:0]; //This switches to `myFirstViewController`
}
In myFirstViewController I have the following methods:
-(void)viewDidLoad
{
[super viewDidLoad];
if (!self.ghhaiku)
{
self.ghhaiku = [[GHHaiku alloc] init];
}
}
- (void) viewWillAppear:(BOOL)animated
{
NSLog(#"%d",self.ghhaiku.justComposed);
if (self.ghhaiku.justComposedYes==YES)
{
[super viewWillAppear:animated];
self.displayHaikuTextView.text = [[self.ghhaiku.arrayOfHaiku lastObject] valueForKey:#"quote"];
}
}
The BOOL in saveUserHaiku in mySecondViewController shows 1/yes. But the Boolean in viewWillAppear in myFirstViewController shows 0/no.
What am I leaving out?
EDIT:
This is what I'm trying to accomplish in the end:
myFirstViewController instantiates GHHaiku in viewDidLoad. Then it creates arrayOfHaiku on that instantiation and loads it with x number of haiku.
The method saveUserHaiku in mySecondViewController adds a haiku to that array, sets a Boolean justComposed' toYES, and then programmatically switches view controllers back tomyFirstViewController`.
Once we're back in myFirstViewController, viewWillAppear calls a certain function if justComposed in mySecondViewController is YES.
Both view controllers were created in Interface Builder.
SECOND EDIT: In case it matters, this is a tabbed application. saveUserHaiku changes the tab.
It appears that you are creating separate instances of ghhaiku - one for each of your two view controllers. If you would like to use the same ghhaiku object between both view controllers (thereby remembering the justComposed boolean), you will need to set the ghhaiku property to the existing object when you create the view controller. Do not alloc/init a new one in viewDidLoad.
For instance, if you were to display mySecondViewControllerClass from myFirstViewController, it might look like the following:
mySecondViewControllerClass *secondViewController = [[mySecondViewControllerClass alloc] initWithNibName:nil bundle:nil];
secondViewController.ghhaiku = self.ghhaiku; // pass the ghhaiku object to the other view controller
[self presentModalViewController:secondViewController animated:YES];
Why should it have other value than NO? You have two different classes. In one of them you set value of a class member to YES. And then in another class you check value of a variable which has the same name, but it's different and you never assign anything to it, and it is NO by default

Need a really simple navigation controller with a table view inside a tab bar controller

I have an app with a tab bar controller (2 tabs). In one tab view controller, a button leads to an alert window. I want one button of the alert window to call a table view containing possible answers. I want that table view to have a done button and a title. I think that means a navigation controller has to be used. But most everything I can find on navigation controllers assumes a much more complicated situation. Here's part of the alert window logic:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
AnswersViewController *aVC = [[AnswersViewController alloc] init];
[self presentViewController:aVC
animated:YES
completion:NULL];
}
}
And AnswersViewController looks like this:
#interface AnswersViewController : UITableViewController
#end
#implementation AnswersViewController
- (id) init
{
self = [super initWithStyle:UITableViewStylePlain];
return self;
}
- (id) initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self view] setBackgroundColor:[UIColor redColor]];
}
#end
This code all works as expected (an empty red UITableView appears).
Two questions I guess: 1. Is there a simple modification to what I have that can give me a done button and title in my table view? 2. If I have to go to a navigation controller (probably), how can I make a bare-bones navigation controller with a done button and title and embed the table view within it? Oh, and I want to do this programatically. And I think I prefer the done button and title to be in the navigation bar, no tool bar desired. Thanks!
To get what you are looking for, you do need to use a UINavigationController. That will provide the UINavigationBar where you can display a title and also buttons.
To implement this with a UINavigationController, you want to do smoothing like this (assuming you are using ARC, so you don't need to worry about memory management):
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
AnswersViewController *aVC = [[AnswersViewController alloc] init];
//Make our done button
//Target is this same class, tapping the button will call dismissAnswersViewController:
aVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissAnswersViewController:)];
//Set the title of the view controller
aVC.title = #"Answers";
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:aVC];
[self presentViewController:aNavigationController
animated:YES
completion:NULL];
}
}
Then you would also implement - (void)dismissAnswersViewController:(id)sender in the same class as the UIAlertView delegate method (based on the implementation I have here).
Hope this helps!

Using a different UIViewcontroller to control UISearchDisplayController

I have a tableview with nsfetchedresultscontroller. I would like to setup search with uisearchdisplaycontroller. I would like to separate the search results into a different viewcontroller file. I am able to do that but if i set the contentestcontroller to another controller other then self then the delegate methods work but the tableview doesn't show. I was able to get this setup working by setting uisearchbar delegate to self and pass the serchtext to the other tableview controller but i would like to have it all working in the search tableview controller.
Do i need to make some connections or in general this would not be a good idea?
Here is some code
This is in viewdidload for the maincontroller
- (void)viewDidLoad
{
[super viewDidLoad];
self.peopleListSearch = [[PeopleListSearchViewController alloc] init];
self.peopleSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.debug = NO;
self.isSearching = NO;
self.peopleSearchBar.delegate = self.peopleListSearch;
self.searchBarController = [[UISearchDisplayController alloc]initWithSearchBar:self.peopleSearchBar contentsController:self.peopleListSearch];
self.searchBarController.searchResultsDelegate = self.peopleListSearch;
self.searchBarController.searchResultsDataSource = self.peopleListSearch;
</code>
This is the main tableview controller which works.
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
self.peopleListSearch.searchTerm = searchBar.text;
[self.peopleListSearch performSearch];
[self.searchBarController.searchResultsTableView reloadData];
}
Is there method that is called to show the tableview of the uisearchdisplay controller.
Thanks in advance!

How to add associated object to uitapgesture?

Currently I have in my code
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addCompoundToLabel:)];
My code would be cleaner if it was something like
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addCompoundToLabel:) withObject:data];
I read that associated objects allow you to pass objects in a gesture recognizer, but after reading the docs, I don't quite understand how to implement it into my code. An example implementation of associated objects would be appreciated. Thanks
EDIT:
this is what the beginning of addCompound looks like
- (void)addCompoundToLabel:(UIGestureRecognizer *)recognizer {
if( [recognizer state] == UIGestureRecognizerStateEnded ) {
FormulaLabel* label = (FormulaLabel*)[recognizer view];
Using associated objects is a messy solution. Instead, you could create a false target object, give it your UIView and your Data, and have access to both of them in the selector method. It is a lot more readable, and your code expresses your intent a lot better.
Here is a quick and dirty example:
#interface FalseTarget : NSObject {
MyViewController *_viewCtrl;
MyData *_data;
}
-(id)initWithViewCtrl:(MyViewController*)viewCtrl andData:(MyData*)data;
-(void)tap:(id)sender;
#end
#implementation
-(id)initWithViewCtrl:(MyViewController*)viewCtrl andData:(MyData*)data {
self = [super init];
if (self) {
_viewCtrl = viewCtrl;
_data = data;
}
return self;
}
-(void)tap:(id)sender {
[_viewCtrl processTapFromSender:sender withData:_data];
}
#end
Add processTapFromSender:withData: method to your controller:
-(void)processTapFromSender:(id)sender withData:(MyData*)data {
// Your action
}
Now you can create your tap recognizer like this:
FalseTarget *target = [[FalseTarget alloc] initWithViewCtrl:self andData:data];
tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:#selector(tap:)];

How would I load a new viewcontroller in an if statement OBJ C

trying to work out how I can load a new viewcontroller from within an if statement in ObjectiveC.
Basically when the app first launches I want to check whether user details have been entered before on a previous occasion, and if not go to a user details screen to do so. If the details have already been entered just proceed to the main menu screen.
My if statement:
if (gGroupDetails != 0)
{
//This will go to default page (main menu)
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
else
{
//This will go to the edit details page
self.window.rootViewController = self.editUser;
[self.window makeKeyAndVisible];
return YES;
}
I am getting the main menu to display okay, but the user details just shows as a blank white screen.
I have set up the nibs for both viewcontrollers.
If anyone can point this newbie in the right direction?
Thanks
It looks like you're doing this from in your AppDelegate. Just use
MyNewViewController *controller = [[MyNewViewController alloc] initWithNibName:#"MyNewViewController" bundle:nil];
[self.window addSubview:controller.view];
I dont know what is gGroupDetails but i assume that its value can be negative.
So you can do it this way, if you have two xib files named as 'MainViewController' and 'EditViewController'
In your .m modify the init method as follows:
(id)init{
NSString* nibName = gGroupDetails > 0 || gGroupDetails < 0 ? #"MainView" : #"EditView";
if (self = [super initWithNibName:nibName bundle:nil]) {
self.wantsFullScreenLayout = YES;
}
return self;
}