Using one ViewController for multiple scenes - iphone

I want to use the same ViewController for multiple scenes. Now I want to implement slightly different behavior depending on which scene is used. I guess this is possible using the identifier. Like (pseudo-code)
if (self.identifier == scene1)
{
// do this
}
else if (self.identifier == scene2)
{
// do that
}
How can I call the identifier from the ViewController?
EDIT:
I mean this identifier from the Inpector - how can I call it in code?
Thanks in advance.

I fixed it. One possible approach is to name the Segway Identifier and then check for equal string in the prepareForSegue method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"HelpSegue"])
{
// do this
}
else
{
// do that
}
}

I think the correct design here would be to use an enumerated value, such as:
typedef enum{
kViewControllerStyle1,
kViewControllerStyle2,
kViewControllerStyle3
} ViewControllerStyle;
Hook up all of the view elements that need adaptation to this style through interface builder outlets and add a switch-case in your "viewDidLoad" method to make the correct adjustments, relying on the current view controller style (also add a "ViewControllerStyle" property to your view controller class).

Related

Detecting the previous view controller

I am currently intending to detect the previous view controller in the -viewDidLoad method, and my intended result would be something like this:
-(void)viewDidLoad
{
if (lastViewController==firstViewController)
{
//do something
}
else
{
//do something else
}
I did previously read about utilizing the viewControllers property of UINavigarioController (and in this case I am using a UINavigationController). However, I don't fully understand how are they supposed to implement such a thing in a if statement.
Do you mean to say you're trying to determine which VC is behind your current VC in the navigationController viewControllers stack? If so, you could use:
if ([self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2]
isEqual:firstViewController]) {
//...

Strange behaviour in viewWillAppear

I have a TabBar Controller with some tab bar item in it.
The first time that a user tap on a tab bar item, I want that a alertview is opened, so that the user can read some little instruction tips.
I have a global variable (say CONFIG), that hold some boolean valeus (CONFIG.tip1AlreadySeen, CONFIG.tip1AllreadySeen, etc.). All these boolean values are initializated to NO.
When the user tap a tab bar item, the viewWillAppear method in its viewcontroller is executed. In this method I put a code like this one:
-(void) viewVillAppear: (BOOL) animated {
extern CONFIG; // <- it's not the actual code but it indicates that a global variable must be used
[super viewWillAppear: animated];
if(CONFIG.tip1AlreadySeen == NO) {
CONFIG.tip1AlreadySeen = YES;
// code for showing the alertview
}
}
The strange thing is that this piece of code works perfectly in one viewcontroller but doesn't work in one another.
With some debug, I fidd out that in the another viewcontroller the code is executed but the assigment CONFIG.tipAlreadySeen = YES doesn't modify the actual value of CONFIG.tipAlreadySeen. This value is still NO. Unbelievable!!!
A little workaround was using the viewDidAppear method for changing the value:
-(void) viewVillAppear: (BOOL) animated {
extern CONFIG; // <- it's not the actual code but it indicates that a global variable must be used
[super viewWillAppear: animated];
if(CONFIG.tip1AlreadySeen == NO) {
// code for showing the alertview
}
}
-(void) viewDidAppear: (BOOL) animated {
extern CONFIG;
CONFIG.tip1AlreadySeen = YES;
}
...But I really did not understand what happened!!! Someone of you could explain this behaviour?
Thanks in advance!
Marco
Why must this be global and not contained in the view controller itself? Just a simple BOOL #property on your view controller that is toggled. And, to maintain this persistent across multiple runs of your application, save out the result to NSUserDefaults, which you in turn check each time you init your view controller.

Adding a button to TTTable View Item

I have been banging my head on adding a button to a cell. This is what I have done.
I subclassed TTTableMessageItem and TTTableMessageItemCell.
I added the following method to the Message Item init :
+ (id)itemWithTitle:(NSString *)title caption:(NSString *)caption text:(NSString *)text timestamp:(NSDate *)timestamp imageURL:(NSString *)imageURL URL:(NSString *)URL target:(id)target action:(SEL)action;
I also added a SEL variable and "id" for action and target.
Under setObject in message Item Cell I added this :
- (void)setObject:(id)object {
if (_item != object) {
[super setObject:object];
TJTableMessageItem* item = object;
if (item.plusAction) {
self.plusAction = item.plusAction;
}
if (item.plusTarget) {
self.plusTarget = item.plusTarget;
}
}
}
I am now able to trigger easily a method inside my datasource for the tableview. But I am not able to find out which cell was pressed. I hope someone can help me, I have spent way to much time figuring out the setObject part.
I would like to know how and add a subview like the Facebook app has, the Like, Comment part. I think I need to be able to run a method inside the view controller. But I can't find anything anywhere. The Cybersam blog has an explanation that doesn't use the TableItem and TableItemCell like Three20 puts things up.
assume that the target is your TableViewController, and the the SEL has an argument named sender, like this:
- (void)likeButtonClicked:(id)sender;
then you can find the Cell View according to sender (sender.superView.....), and use the TableView to find the cell's index, that's all.

Multiple UIWebViews question

I'm really not good with these protocols, especially because this is really my first time using them. I have two UIWebViews in the same view: webView and webView2. How do I change this line of code to work for webView2?
-(void)webViewDidStartLoad:(UIWebView *)webView {
I tried changing "webView" to "webView2", but Xcode said that I had the same line twice, so obviously this won't work. What should I do? Thanks for your help!
Btw, I'm thinking I have to add an IF statement within here, but what should it be?
The delegate method passes in a parameter defining which web view it’s coming from for exactly this reason.
- (void)webViewDidStartLoad:(UIWebView *)theWebView
{
if(theWebView == webView)
{
// do something
} else if(theWebView == webView2)
{
// do something else
}
}
Well the same method will be called, but the ref of the webview is passed in,
So you can keep a reference to both your webviews in your delegate and say something like if(webview1==webview) which will evaluate to yes only if the webview ref passed in the delegate method is webview1 so you can figure out which webview is calling the delegate using the if statement
I think you want:
- (void)webViewDidStartLoad:(UIWebView *)webView; {
if(webView == webView1){
// use the first webview here.
}
if(webView == webView2){
// use the second webview here.
}
}
Hope that Helps!

How to know or retrieve the sender id

I have the following method
-(IBAction)back:(id)sender {
}
and would like to be able to know the sender id.
e.g. if there are multiple buttons linked to this method, I would like to know which button was pressed.
Simply use the tag property, inherited from UIView, which is a NSInteger, in a switch statement, or using if conditions.
The tag property can be set in your code, or through InterfaceBuilder.
[sender tag]
I don't know what you mean by "id" (the "sender" is an id, effectively an NSObject *), but you could use tags. You have to set the tag beforehand in Interface Builder or programmatically.
If you have set up IBOutlets for the buttons in your interface then you can simply compare the sender to those.
That is in your interface definition if you have
...
(IBOutlet) UIButton *button1;
(IBOutlet) UIButton *button2;
...
and in your implementation you have:
- (IBAction) buttonPressed: (id) sender
{
if (sender == button1) {
....
}
else if (sender == button2) {
...
}
}
Personally, I'd prefer to use different action methods for each button and then they can all call a common routine for the things that are common. However, for simple projects the above will work.
-J
Set the tag property of each button to a unique integer (either in IB or programmatically) and switch on it inside your action method.